We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11793
    • 49 Posts
    Should this work? Please assume that all aliases used here are valid and defined in the XML:

    $obj->getOne('Alias1');
    
    $obj->Alias1->getOne('Alias2');
    
    $obj->Alias1->Alias2->getOne('Alias3');
    
    


    Because that doesn’t work for me. Now, the fly in the ointment is that $obj (above) is obtained via getCollection().

    UPDATE: In my code, $obj is in fact derived via getOne() from an object obtained via getCollection(). So, line 1 (above) does work, but lines 2 & 3 do not.

    Is it possible that objects obtained via getCollection are precluded from using getOne()?

    xPDO 2.0.0-rc2 (version currently shipping) on Evo 1.0.4, PHP 5.2.13, MySQL 5, Apache 2.2, Win2K3
      • 3749
      • 24,544 Posts
      Try it this way:

      $obj1 = $obj->getOne('Alias1'); // (or getCollection()).
      
      $obj2 = $obj1->getOne('Alias2');
      
      $obj3 = > $obj2->getOne('Alias3');


      If that doesn’t work, it’s possible that there’s something wrong with your schema.

      You could also use getCollectionGraph() for the first step to get Alias1 and Alias2 in a single query (I don’t know if you could get Alias3 at the same time). That would look something like this:

      $objects = $modx->getCollectionGraph('myObject', array('Alias2' => array()),$criteria));
      
      foreach($objects as $object) {
        $output .= 'Alias one Field: ' . $object->get('fieldname') . '<br />';
        $output .= 'Alias two Field: ' . $object->Alias2->get('fieldname') . '<br /><br />';
        $obj3 = $object->Alias2->getOne('Alias3');
        $outout .= 'Alias three Field: ' . $obj3->getOne('fieldname');
      }


        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
        • 21257 MODX Staff
        • 730 Posts
        Have you set the constant xPDO::OPT_HYDRATE_RELATED_OBJECTS?
        $xpdo->setOption(xPDO::OPT_HYDRATE_RELATED_OBJECTS, true);
        

        Without that option, related objects are only accessible as members of the _relatedObjects array.
          Mike Schell
          Lead Developer, MODX Cloud
          Email: [email protected]
          GitHub: https://github.com/netProphET/
          Twitter: @mkschell