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

    I have a requirement to run a query something like

    SELECT * FROM exampleTable WHERE A = 1 AND (B LIKE 'example' OR B LIKE 'text')
    


    I’ve looked at the andCondition and the orCondition but I’m not clear on how to put the brackets round the OR.
    Sorry if this has been asked before, but I just couldn’t seem to find the right search phrase.

    Thanks
      • 25284
      • 141 Posts
      Hi, I’ve found a work around by using direct SQL.
      I’d be really interested to know how to do this with xPDO though


      I think I’m starting to get it..

      This has brackets...
      $query->where($where, xPDOQuery::SQL_AND);
      



      followed by another AND outside brackets
      $query->where($queryAnd);
      


      So to get my whole set of ’OR’ criteria in brackets, would I just use
      $query->where($queryOr, xPDOQuery::SQL_AND)
      


      ?
        • 21257 MODX Staff
        • 730 Posts
        You can easily nest conditions with arrays.
        So this query:
        SELECT * FROM exampleTable WHERE A = 1 AND (B LIKE 'example' OR B LIKE 'text')

        could be written like this:
        $query->where(array(
            'A'=> 1,
            array(
                'B:LIKE'=> 'example',
                'OR:B:LIKE'=> 'text'
            )
        ));
        
          Mike Schell
          Lead Developer, MODX Cloud
          Email: [email protected]
          GitHub: https://github.com/netProphET/
          Twitter: @mkschell
          • 25284
          • 141 Posts
          Brilliant! Thanks.