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

    Having joined tables like so:
    $c = $modx->newQuery('Myobject');
    $c->innerJoin('MyJoin','MyJoin');

    $myobject = $modx->getObject('Myobject', $c);

    How do I now access the vales from the joined table?
    If I use $myobject->get('blah') = blank
    If I use $myobject->get('MyJoin.blah') = blank

    Any ideas?

    Cheers,

    J
      • 22303 MODX Staff
      • 10,725 Posts
      You don't really, though extra fields should be set. In order to access the related objects properly, you would need to load those as objects; this is what graphs are for. That said, I think you are missing your join conditions...

      The easier way to do this and do it in one query, is using getObjectGraph() —
      $object = $modx->getObjectGraph('MyObject', array('MyJoin' => array()));


      Then, if the related objects exist and the relations are defined properly, you can simply reference the fields from MyJoin like so:
      $object->MyJoin->get('blah');

        • 15083
        • 697 Posts
        thanks for reply.
        Can u pass WHERE conditions into the graph?
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: jusmeig at Jun 27, 2012, 02:36 PM
          Can u pass WHERE conditions into the graph?
          Yes, it has the same parameters as getObject except the second parameter $graph is inserted before the $criteria parameter. So the third parameter of getObjectGraph is $criteria and the fourth is $cacheFlag —

              /**
               * Retrieves an xPDOObject instance with specified related objects.
               *
               * @uses xPDO::getCollectionGraph()
               * @param string $className The name of the class to return an instance of.
               * @param string|array $graph A related object graph in array or JSON
               * format, e.g. array('relationAlias'=>array('subRelationAlias'=>array()))
               * or {"relationAlias":{"subRelationAlias":{}}}.  Note that the empty arrays
               * are necessary in order for the relation to be recognized.
               * @param mixed $criteria A valid xPDOCriteria instance or expression.
               * @param boolean|integer $cacheFlag Indicates if the result set should be
               * cached, and optionally for how many seconds.
               * @return object The object instance with related objects from the graph
               * hydrated, or null if no instance can be located by the criteria.
               */
              public function getObjectGraph($className, $graph, $criteria= null, $cacheFlag= true) {
          
            • 15083
            • 697 Posts
            Thanks man, for the life of me...I cant figure out who to iterate over the results.
            I have done the joins etc, and if I view the SQL I can see that the query being run is returning 56+ lines of data.

            How all the below fails:

            $object = $modx->getObjectGraph('MyObject', array('MyJoin' => array()));

            foreach($object as $this_guy) {}

            I have managed to get the first line of data by using:
            print_r($object->toArray());

            I can't seem to loop anything, or get the rowcount returned to do my logic, counting object returns 1?
              • 15083
              • 697 Posts
              I should be arrested and put in prison for my stupidity :0
              getCollectionGraph !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!