We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    Having some difficulty trying to find the correct syntax to join multiple related tables in an xPDO query:

    $mygroups = $this->getGroupMemberships();
    
    $criteria = $this->modx->newQuery('Contract');
    
    $criteria->leftJoin('Client','Client');
    
    $criteria->leftJoin('Program','Program');
    
    $criteria->leftJoin('Location','Location', array('Program.location_id:=' => 'Location.id'));
    
    $criteria->where(array('Contract.status_id' => 2,'OR:Contract.status_id:=' => 3));
    
    $criteria->where(array('Client.first_name:LIKE' => "%$searchQuery%",'OR:Client.cams:LIKE' => "%$searchQuery%",'OR:Client.last_name:LIKE' => "%$searchQuery%"));
    
    $criteria->where(array('Location.location_id:IN' => $mygroups));
    
    
    $options = $this->modx->getCollectionGraph('Contract', '{ "Client": {}, "Program": { "ProgramName": {}, "Location": {}, }, }', $criteria);
    
    $criteria->prepare();
    
    $this->modx->log(modX::LOG_LEVEL_ERROR,'Running "getJsonPayees", sql = '.$criteria->toSQL());




    But when I view the query it gives me:

    SELECT (a whole lot of fields!)
    FROM `flow_contract` AS `Contract` 
    LEFT JOIN `flow_client` `Client` ON `Contract`.`client_id` =  `Client`.`id` 
    LEFT JOIN `flow_program` `Program` ON `Contract`.`program_id` =  `Program`.`id` 
    LEFT JOIN `flow_location` `Location` ON `Program`.`location_id` = 'Location.id'
    
    WHERE  (  ( `Contract`.`status_id` = 2 OR `Contract`.`status_id` = 3 )  
    AND  ( `Client`.`first_name` LIKE '%k%' OR `Client`.`cams` LIKE '%k%' OR `Client`.`last_name` LIKE '%k%' )  AND `Location`.`id` IN (1,2,3,4,5,6,7,8,9,10) )



    If I change:

    LEFT JOIN `flow_location` `Location` ON `Program`.`location_id` = 'Location.id'


    To

    LEFT JOIN `flow_location` `Location` ON `Program`.`location_id` = `Location`.`id`



    Then it works perfectly ... what is the correct syntax to make that happen?


      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 15083
      • 697 Posts
      U could not use raw PDO as a work around.
      In my experience I have found this much faster than pulling back a bunch of objects (im not a huge fan of ORMs/b's in general)

      $q = 'SELECT (a whole lot of fields!)
      FROM `flow_contract` AS `Contract` 
      LEFT JOIN `flow_client` `Client` ON `Contract`.`client_id` =  `Client`.`id` 
      LEFT JOIN `flow_program` `Program` ON `Contract`.`program_id` =  `Program`.`id` 
      LEFT JOIN `flow_location` `Location` ON `Program`.`location_id` = 'Location.id'
       
      WHERE  (  ( `Contract`.`status_id` = 2 OR `Contract`.`status_id` = 3 )  
      AND  ( `Client`.`first_name` LIKE '%k%' OR `Client`.`cams` LIKE '%k%' OR `Client`.`last_name` LIKE '%k%' )  AND `Location`.`id` IN (1,2,3,4,5,6,7,8,9,10) )';
      
      
      $query = new xPDOCriteria($modx, $q);
      $query->prepare();
      $query->stmt->execute();
      
      $myrows = $query->stmt->fetchAll(PDO::FETCH_ASSOC);


      Downside: You are working with raw PDO, so no nice objects returned.
      Plus side: faster. Its much faster! You get an array of results back.
        • 4172
        • 5,888 Posts
        try

        $criteria->leftJoin('Location','Location', 'Program.location_id = Location.id');
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 26503
          • 620 Posts
          @jusmeig
          Perhaps if we did something like this:

          PDO::FETCH_OBJ: returns an anonymous object with property names that correspond to the column names returned in your result set

          Working with the object after the query is much nicer...

          @bruno17
          then that results in the join looking like: LEFT JOIN `flow_location` `Location` ON Program.location_id = Location.id which does work, but is not formatting the same as the other joins [ LEFT JOIN `flow_program` `Program` ON `Contract`.`program_id` = `Program`.`id` ] leaving me thinking that there is a 'right' way to do it?

            *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

            Sean Kimball CLP, CLS.
            Technical Director / Sr. Developer | BigBlock Studios
            ._______________________________________________.
            Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
            27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
            phone/fax: 905-426-5525