We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27738
    • 2 Posts
    Hi Peeps - Can you help me please?

    I’m using newquery to retrieve records across four tables:
    User -> Story -> StorySentances ->Sentance
    $c = $modx->newQuery('Users'); 
    $c->select('Users.user_id, STY.sty_title, STYSTC.stystc_sentance_id, STC.stc_text');
    $c->innerJoin('Story','STY','Users.user_id = STY.sty_owner_id'); 
    $c->innerJoin('StorySentances','STYSTC','STY.sty_id = STYSTC.stystc_story_id'); 
    $c->innerJoin('Sentance','STC','STYSTC.stystc_sentance_id = STC.stc_id');
    $c->where(array(
        'Users.user_id' => 43, )); 
    $stories = $modx->getCollection('Users',$c);
      foreach ($stories as $s)
      {
        $output .= '<br />'.$s->get('user_id');
        $output .= '<br />'.$s->get('sty_title');
        $output .= '<br />'.$s->get('stystc_sentance_id');
        $output .= '<br />'.$s->get('stc_text');
      }
    $output .= '<br /><br />'.$c->toSQL(); 
    return $output;
    

    It works but only gives me the very last record.
    43
    Personal space
    24
    When I give people enough space, I get to play with and talk to people, I make friends and have fun. 

    The sql generated is:
    SELECT Users.user_id, STY.sty_title, STYSTC.stystc_sentance_id, STC.stc_text FROM `shl_users` AS `Users` JOIN `shl_story` `STY` ON Users.user_id = STY.sty_owner_id JOIN `shl_story_sentances` `STYSTC` ON STY.sty_id = STYSTC.stystc_story_id JOIN `shl_sentance` `STC` ON STYSTC.stystc_sentance_id = STC.stc_id WHERE `Users`.`user_id` = 43 
    

    Which gives me all the records if I run it directly against the tables through a client:

    If I try the same query but using the following:
    $users = $modx->getObject('Users',43); 
    $output .= $users->get('user_id');   
    $stys = $users->getMany('Story');   
    $output .= '<br /><br />Stories:';
    foreach ( $stys as $s ) 
    {
        $output .= '<br />Storiy Title - '.$s->get('sty_title');
        $styjoin = $s->getMany('StorySentances');   
        foreach ( $styjoin as $s ) 
        {
           $stystc = $s->getMany('Sentance');
           foreach ($stystc as $c)
           {
             $output .= '<br />'.$c->get('stc_text');
           }
        } 
         $output .='<br />';
    } 
    

    It gives me all the records as well
    43
    
    Stories:
    Storiy Title - How To Listen Well
    It's important to look at people and stop what I'm doing when they have something to tell me.
    Sometimes grown-ups tell me very important things that I need to know.
    If I don't look & listen I might miss something important and make the grown-ups angry.
    I know it's wrong to keep doing what I'm doing when grown-ups want me to listen.
    I will listen to grown-ups when they talk to me.
    
    Storiy Title - Personal space
    When I talk to people I need to give them their space and stay away from their faces.
    When people come too close it makes other people uncomfortable.
    Everybody needs space.
    When I make people uncomfortable, they want to get away from me.
    They might not want to ever talk to me again.
    When I give people enough space, I get to play with and talk to people, I make friends and have fun. 
    

    So where am I going wrong with newQuery - what am I missing?

    Any help would be much appreciated - smiley
      • 6228
      • 249 Posts
      As far as I know, you are missing some fields in your select statement.

      STYSTC.stystc_story_id, STC.stc_id, and all other fields referenced in the ON clauses need to be included in the select fields.
        lo9on.com

        MODx Evolution/Revolution | Remote Desktop Training | Development
        • 27738
        • 2 Posts
        Hi Cylissmo,

        Thanks very much for the reply smiley

        I’ve had a play with what you suggested but no joy I’m afraid. So I started again from scratch.
        If I remove the Users table from the query (Story ->StorySentances->Sentance) it works as it should do and returns all the correct records.
        I then renamed the Users table to MyUsers just in case the name was causing an issue, wrote a newQuery on just the MyUsers table and it returned the correct number of records.
        $c = $modx->newQuery('MyUsers'); 
        $c->select('MyUsers.user_id');
        $stories = $modx->getCollection('MyUsers',$c);
        $output .= 'count: '.count($stories);
          foreach ($stories as $s)
          {
            $output .= '<br />user id'.$s->get('user_id');
          }
        

        I then added Story onto the MyUsers query and it only returned the last record huh
        $c = $modx->newQuery('MyUsers'); 
        $c->select('MyUsers.user_id, STY.sty_id, STY.sty_owner_id, STY.sty_title');
        $c->innerJoin('Story','STY','MyUsers.user_id = STY.sty_owner_id'); 
        $stories = $modx->getCollection('MyUsers',$c);
        $output .= 'count: '.count($stories);
          foreach ($stories as $s)
          {
            $output .= '<br />user id'.$s->get('user_id');
           $output .= '<br />sty id'.$s->get('sty_id');
            $output .= '<br />sty owner id'.$s->get('sty_owner_id');
            $output .= '<br />sty title'.$s->get('sty_title');
          }
        


        This is my schema
        	<object class="MyUsers" table="my_users" extends="xPDOObject">
        		<field key="user_id" dbtype="int" precision="11" phptype="integer" null="false" index="pk" generated="native" />
        		<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" >
        			<column key="user_id" length="" collation="A" null="false" />
        		</index>
        		<composite alias="UserDescriptions" class="UserDescriptions" local="user_id" foreign="usrdesc_userid" cardinality="one" owner="local" />
        		<composite alias="Story" class="Story" local="user_id" foreign="sty_owner_id" cardinality="one" owner="local" />
        	</object>
        	<object class="Story" table="story" extends="xPDOObject">
        		<field key="sty_id" dbtype="int" precision="11" phptype="integer" null="false" index="pk"  generated="native" />
        		<field key="sty_owner_id" dbtype="int" precision="11" phptype="integer" null="false" default="0" />
                             .
                             .
                             .
        
        		<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" >
        			<column key="sty_id" length="" collation="A" null="false" />
        		</index>
        		<composite alias="StorySentances" class="StorySentances" local="sty_id" foreign="stystc_story_id" cardinality="one" owner="local" />
        		<aggregate alias="MyUsers" class="MyUsers" local="sty_owner_id" foreign="user_id" cardinality="many" owner="foreign" />
        		<composite alias="UserDescriptions" class="UserDescriptions" local="sty_id" foreign="usrdesc_userid" cardinality="one" owner="local" />
        	</object>
        


        I have absolutely no idea why this is a problem!!!!!!!
        Any suggestions greatfully recieved before my sanity gives out grin