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

    Is it possible to get related data via getCollectionGraph() and then use something like

    [[+customer.warehouse]] 
    



    in a template to access the nested/related data?

    In other words, if I fetch a record that has relational data, and need to use fields from the related record in the chunk, I currently do:
    $obj = $modx->getCollectionGraph('Workorder', '{ "Team":{}}', $id);
    $data = $obj->toArray();
    $data['team_name'] = $obj->Team->get('name');
    $html = $modx->getChunk('workorder_form', $data);
    

    which can get cumbersome with deeply related data. Is it possible to do something simpler like pass the obj returned from getCollectionGraph() to getChuck and use a placeholder syntax like
    [[+Team.name]]
    

    in the chunk?
    [ed. note: pallen last edited this post 14 years, 11 months ago.]
      • 22303 MODX Staff
      • 10,725 Posts
      You'll have to wrap your example in code tags for us to see it. Otherwise, the MODX tags and HTML will get stripped.
        • 36692
        • 13 Posts
        Hello opengeek,

        I've updated the post with code tags and better explained what I'm asking. Perhaps I'm missing some trick in how you're supposed to render related data in Chunks.

        -Pete
          • 22303 MODX Staff
          • 10,725 Posts
          What you set in the code would be rendered as this in the Chunk tpl:

          [[+team_name]]


          ?
            • 36692
            • 13 Posts
            Correct, but what I'm asking is if you use getCollectionGraph and get back, say, a workorder with a related team (we service NASCAR race teams), and want to show the team name in the chunk output.

            Is there an easier way than to "flatten" the nested data into a flat assoc. array like:

            $data['team_name'] = $obj->Team->get('name');
            


            A little hard to explain I guess, how to properly render collectiongraph data in Tpl chunks...

              • 22303 MODX Staff
              • 10,725 Posts
              At the moment you have to flatten it. However, in the upcoming 2.2 releases, you can tell toArray() to return related object columns as nested arrays, so you could do:

              <?php
              $obj = $modx->getObjectGraph('Workorder', '{ "Team":{}}', $id);
              $data = $obj->toArray('', false, true, true);
              $html = $modx->getChunk('workorder_form', $data);
              

              NOTE: I think you want getObjectGraph, not getCollectionGraph (which returns an array of objects)...

              Then you can reference:
              [[+Team.name]]

              in the chunk.