We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22295
    • 153 Posts
    This fragment:

    		$query->where(array(	
    		 	'userId' => $ownerId,
    			'class' => 'post'
    		), xPDOQuery::SQL_AND,null,1);  
    
    		$query->andCondition(array(
    		 	'relatedToUserId' => $ownerId,
    			'class' => 'comment'
    		), null,2);


    Produces this sql code:
     WHERE ( `act`.`userId` = '171' AND `act`.`class` = 'post' ) AND ( `act`.`relatedToUserId` = '171' AND `act`.`class` = 'comment' ) 


    That’s correct, but I can not find the way to get the (groups) with OR between them, ie.
    note - i still need the AND inside each group.
     WHERE ( `act`.`userId` = '171' AND `act`.`class` = 'post' ) OR ( `act`.`relatedToUserId` = '171' AND `act`.`class` = 'comment' ) 





    for the record - this alternative for the second group does NOT help - it produces the same sql as above:
    $query->orCondition(array( ’userId’ => $ownerId ),null,2);
    $query->andCondition(array(’class’ => ’relationship’),null,2);
      • 22295
      • 153 Posts
      Ok, I partially answer myself.

      The thing is here:
      xpdoquery.class.php, line 490: buildConditionalClause()
      $conjunction= xPDOQuery::SQL_AND

      This controls what I’m looking for. setting to .._OR and I get the correct results:
      (x AND y) OR (x AND y) OR (x AND y)

      Now, how to change this per query, and not in the source...
      Browsing through the code, I see it is not currently possible to independently control this param.

      foreach ($conditions AS $i => $condition) {
      	$where= $query->buildConditionalClause($condition,xPDOQuery::SQL_AND);
      	$query->where($where, xPDOQuery::SQL_OR,null,$i);
      }


      The above approach should resolve is, but ->buildConditionalClause doesn’t work (returns this strange: ( u ) AND ( p p )


      • oori:

        I’m aware of the problem and will work on a solution in conjunction with some other improvements in this area. If you want to log a ticket at http://svn.modxcms.com/jira/browse/XPDO we can make this an official bug. wink

        Cheers!
          • 22295
          • 153 Posts
          Thanks for your reply.
          I’ve logged it in JIRA: http://svn.modxcms.com/jira/browse/XPDO-38
          Have you a quick suggestion on how to bypass that "for now"?

          Thanks again.
          • For now, simply write out the expressions; you can still use prepared statements this way:
            <?php
            $query->where("(`act`.`userId` = :userid AND `act`.`class` = :class ) OR ( `act`.`relatedToUserId` = :userid2 AND `act`.`class` = :class2 )");
            $query->prepare(array(
                'userid' => array('value' => $userId, 'type' => PDO::PARAM_INT)
                ,'class' => array('value' => 'modDocument', 'type' => PDO::PARAM_STR)
                ,'userid2' => array('value' => $userId, 'type' => PDO::PARAM_INT)
                ,'class2' => array('value' => 'modDocument', 'type' => PDO::PARAM_STR)
            ));
            ?>
              • 22295
              • 153 Posts
              As a reference for future-seekers, an example how to resolve this with Jason’s patch is here: http://svn.modxcms.com/jira/browse/XPDO-38