We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 54420
    • 1 Posts
    Quote from: Bruno17 at Dec 14, 2015, 12:31 AM
    Not sure, if it is good, to have the packageName starting with uppercase.
    I think, its better, to have everthing lowercase.
    In this case, you can try

    [[migxLoopCollection?
    &packageName=`Catalog`
    &classname=`Data1`
    &joins=`[
    {"alias":"Data2","classname":"Data2","on":"Data2.model_number=Data1.model_number"},
    {"alias":"Data3","classname":"Data3","on":"Data3.model_number=Data1.model_number"}
    ]`
    &tpl=`yourTplChunk`
    &where=`{"Data1.model_number":"themodelnumber"}`
    ]]


    you will have all the Data1-fields without prefix and the other fields with prefix Data1_ and Data2_

    I appreciated your comment. Thanks for sharing awesome info. :)
    • discuss.answer
      • 54484
      • 1 Posts
      Quote from: BobRay at Dec 13, 2015, 11:52 PM
      If (and it's a big if) you've properly created the tables as related objects of each other, you can use getCollectionGraph() to get them all in a single query. You can also use $query->leftJoin() with getCollection().

      Here's getCollectionGraph() example for users that includes the user profile and custom data. In the example, Profile and Data are aliases of the two user-related objects. The result is an array of $userObjects, each with its own $user->Profile and $user->Data object containing the fields for that user from the other tables:

      $c = $modx->newQuery('modUser');
      $c->sortby('fullname', 'ASC');
      $c->where(
         array('Data.last_name' => 'Johnson'),
      );
      $users = $modx->getCollectionGraph('modUser', '{"Profile":{},"Data":{}}', $c);


      In the second argument to getCollectionGraph(), you can have as many related objects as you need in the JSON section.

      Remove the $c->where() part of you want all of them.

      To process the results you can do something like this (off the top of my head, so possibly not correct):

      foreach ($users as $user) {
         $pFields = $user->Profile->toArray();
         $dFields = $user->Data->toArray();
         $ufields = $user->toArray();
      
         $allFields = array_merge($pFields, $dFields, $uFields);
      
         $output .= $modx->getChunk('MyTpl', $allFields);
      }


      Be sure to put the $user fields as the last argument in the array_merge() so that the user ID will be correct.

      Thanks for updating this thread. It's helpful smiley