We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44195
    • 293 Posts
    I'm still trying to get my head around processors. What is the best way to access multiple tables from a single processor?

    For example, say I have one table called "galleries" and one table called "artworks".
    A gallery can have many artworks but an artwork can only have one gallery.

    I want to make it so a gallery cannot be removed if it has artworks associated with it. Here's a simplistic example schema:
    <?xml version="1.0" encoding="UTF-8"?>
    <model package="example" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
    
        <object class="artworks" table="artworks" extends="xPDOSimpleObject">
            <field key="artworkname" dbtype="varchar" precision="255" phptype="string" null="true" default="" />
            <field key="galleryname" dbtype="varchar" precision="255" phptype="string" null="true" default="" />
            <aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign" />
        </object>
    
        <object class="galleries" table="galleries" extends="xPDOSimpleObject">
            <field key="galleryname" dbtype="varchar" precision="255" phptype="string" null="true" default="" />
            <aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign" />
        </object>
    
    </model>
    


    When an artwork is created, the associated gallery is obviously stored in the "galleryname" field.

    If I have a processor that extends the modObjectRemoveProcessor and has the classKey of "galleries", I assume I need to add a test to see if there are any artworks with that gallery name somewhere in the initialize() or beforeSet() functions.
    Or is it best suited in a different function? Or not in one at all?

    I've tried something along the lines of:
    public function beforeSet() {
        $galleryname = $this->getProperty('galleryname');
        $total = $this->modx->getCount('artworks',array('galleryname' => $galleryname));
        if ($total > 0) {
            return $this->failure($this->modx->lexicon('refuse_delete_items_exist'));
        }
    }
    

    but it didn't work.

    Is this the right kind of approach to take? Or am I going about it the wrong way?

    This question has been answered by BobRay. See the first response.

    [ed. note: muzzstick last edited this post 12 years, 6 months ago.]
      I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
      Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
    • discuss.answer
      • 3749
      • 24,544 Posts
      It looks like the right approach to me.

      Try this:

      public function beforeSet() {
          $galleryname = $this->getProperty('galleryname');
          $c = newQuery('artworks');
          $c->where(array('galleryname' => $galleryname));
          $c->prepare();
          $total = $this->modx->getCount('artworks', $c);
          if ($total > 0) {
              return $this->failure($this->modx->lexicon('refuse_delete_items_exist'));
          }
      }
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 44195
        • 293 Posts
        Fantastic! Thanks very much Bob.

        At first it still wasn't working until I noticed it should have been in the beforeRemove() function rather than beforeSet().
        Hopefully it all sinks in soon and I can stop floundering haha.
        All good now smiley
          I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
          Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
          • 44195
          • 293 Posts
          One more thing... wink

          Although it's working as expected with the above code, instead of a lexicon value being returned saying the gallery can't be removed, I'm getting an error box that just says [object Object].

          Any idea what might be causing this?
            I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
            Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
            • 4172
            • 5,888 Posts
            what is the response (inspected by the developer-tools-console or firebug)
            when you get [object Object] in the error-box?

            how do you fire the ajax-request in your CMP (the ExtJs - Code)?
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 44195
              • 293 Posts
              Hi Bruno,

              The response is:
              {"success":false,"message":{"success":false,"message":"refuse_delete_items_exist","total":0,"errors":[],"object":[]},"total":0,"data":[],"object":[]}
              


              and the extjs code I'm using to call the processor is:
              ,removeGallery: function() {
                  MODx.msg.confirm({
                      title: _('mycomponent.gallery_remove')
                      ,text: _('mycomponent.gallery_remove_confirm')
                      ,url: this.config.url
                      ,galleryname: this.config.galleryname
                      ,params: {
                          action: 'mgr/gallery/remove'
                          ,id: this.menu.record.id
                          ,galleryname: this.menu.record.galleryname
                          ,dir: this.menu.record.url
              
                      }
                      ,listeners: {
                          'success': {fn:this.refresh,scope:this}
                      }
                  });
              },
              


              Cheers
                I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
                Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
                • 4172
                • 5,888 Posts
                not sure, if this does help in your case.
                For MIGXdb - CMPs, I have this success - function:
                https://github.com/Bruno17/MIGX/blob/master/core/components/migx/templates/mgr/gridpanel.tpl#L106
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 44195
                  • 293 Posts
                  Ah thanks Bruno. I actually managed to fix the problem.

                  For some reason (I'd love to find out why) if I returned the failure from beforeSave() or initialize() it returned the "object Object" error.
                  I decided to try putting the test directly into the process() function and it's now working perfectly. Yay!

                  Cheers Bruno and Bob for all the assistance!
                    I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
                    Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com