We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24676
    • 60 Posts
    Hi

    I am having a few problems with a database query.

    I have a plugin which records an action every time a resource is saved into a custom table.

    My snippet retrieves rows and displays them on the related pages. That all works fine, but I also have a series of static resources that are children of the resource. I want those actions to show up on the resource page as well. I hope that makes sense.

    bare with me if I am approaching this completely the wrong way but i am grabbing all the ids into an array like this

    $resourceId = $modx->resource->get('id');
    $array_ids = $modx->getChildIds($resourceId);
    
    array_push($array_ids, $resourceId);


    but how do I pass these ids into my query?

    $actions = $modx->getCollection('Useractions',array('resource_id'=> array($resourceId)));


    I have been going round in circles with this but don't know if I am miles off or nearly there!?
      • 3749
      • 24,544 Posts
      Is 'Useractions' your own object class (there isn't one in MODX) and have you generated the class and map files to make it xPDO-friendly?

      If so, I think you could do it in a single query with a join, (or possibly getObjectGraph() if your schema allows it), but since you already have the IDs in an array, this might be what you want:

      $c = $xpdo->newQuery('userAction');
      $c->where(array(
          'resource_id:IN' => $array_ids)); 
      $actions = $modx->getCollection('Useractions', $c);
      
        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
        • 24676
        • 60 Posts

        Quote from: BobRay at Dec 13, 2013, 04:48 PM
        Is 'Useractions' your own object class (there isn't one in MODX) and have you generated the class and map files to make it xPDO-friendly?

        If so, I think you could do it in a single query with a join, (or possibly getObjectGraph() if your schema allows it), but since you already have the IDs in an array, this might be what you want:

        $c = $xpdo->newQuery('userAction');
        $c->where(array(
            'resource_id:IN' => $array_ids)); 
        $actions = $modx->getCollection('Useractions', $c);
        

        Hi Bob

        Thank you so much for your response. I was struggling all day I should have asked earlier!

        I did have my own class and schema and so your solution (above) was pretty much spot on apart from for some reason the line:

        $c = $xpdo->newQuery('userAction');


        seemed to cause an error. (white screen). As I understood it the 'userAction' should have been my class name but it still didn't work. I replaced $xpdo-> with $modx-> and I got the result I wanted but I can't work out why?

        I am desperately struggling to understand xpdo but there are still many key pieces missing in my knowledge!
          • 3749
          • 24,544 Posts
          The $modx class extends the $xpdo class, so you can generally call all xPDO methods with $modx-> except in rare places where $modx is not instantiated like build-script resolvers.

          It looks like in your plugin, it's the other way around -- $modx is available, but the plugin is not aware of $xpdo. My general rule is to always use $modx, unless it doesn't work.

            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