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

    I try to add the following WHERE logic to a query and struggle to get this working (conditions simplified to a,b,c,d)

    WHERE (a and b) and (c or d)


    ...
    $where = array('condition_a' => '1',   // Condition group 0
                   'condition_b' => '1');  
    $query->where($where);
    $query->andCondition(array('condition_c' => '1'), null, 1); // AND Conjunction should apply between groups
    $query->orCondition(array('condition_d' => '1'), null, 1);  // OR conjunction should apply within condition group 1 only
     ...
    


    XPDO debug and buildConditionalClause report a wrongly build condition statement and I get
    WHERE ((a and b) or (c or d))
    . The and conjunction is discarded.

    I am using Revo 2.0.8pl.

    What would be the correct sequence of statement to achieve this and/or logic?
      • 12512
      • 42 Posts
      If I change the order and have the OR group first then this works:

      $query->andCondition(array('condition_c' => '1'), null, 1); // AND Conjunction should apply between groups
      $query->orCondition(array('condition_d' => '1'), null, 1);  // OR conjunction should apply within condition group 1 only
      $query->where($where);
      


      yields:

      WHERE ((c or d) and (a and b))


      Further experimenting reveals that it appears that a condition group can not have a different conjunction than the one used to link the condition group with the previous group unless it is the very first group.

      This appears to affect also the getResource snippet which is using orCondition() for the tvFilter. Once two TV filters are used an orCondition is inserted but the query fails in a similar way with the linking AND condition being replaced with OR. I checked by inserting
      error_log('cond=' . $criteria->buildConditionalClause($criteria->query['where']));
      into the getResource snippet code.

      Maybe there is deeper problem here in the conjunction grouping mechanism?
        • 33968
        • 863 Posts
        I think you might be right - similar issues reported here:

        http://modxcms.com/forums/index.php?topic=52572.0.
        http://modxcms.com/forums/index.php/topic,59577.msg338990.html#

        And on gitHub (getResources):
        https://github.com/opengeek/getResources/issues/9

        Let’s see if anyone can point out a problem with the way we are going about it, otherwise it needs to be filed as a bug.

        For now I guess you could get by with a complex where condition (xPDO Docs).
          • 12512
          • 42 Posts
          I could build some test cases to identify what is going on under the hood an lodged a bug: http://bugs.modx.com/issues/4877

          I am now using direct SQL as a work around and avoid andCondition/orCondition calls for the near future.
            • 22303 MODX Staff
            • 10,725 Posts
            This can be done using the embedded array syntax:
            <?php
            $query->where(array(
                array(
                    'a' => 1,
                    'b' => 1
                ),
                array(
                    'OR:c:=' => 1,
                    'd' => 1
                ),
            ));

            If you specify an OR on the first condition within a nested array of conditions, this is used as the conjunction between it and any others at that level.
              • 12512
              • 42 Posts
              Hi OpenGeek.

              using the embedded array syntax is a way to achieve the outcome with nested conjunctions and this does solve my initial question. Thank you.

              But what about the use of the andCondition/orCondition calls which also provide the grouping parameter to achieve the same thing? Is andCondition/orCondition with grouping deprecated and should nested where be the preferred way? Shouldn’t below notation yield the same result as your embedded array syntax?
              $query->andCondition(array('a' => '1'));          // First group 0
              $query->andCondition(array('b' => '1'));          // 
              $query->andCondition(array('c' => '1'), null, 1); // AND Conjunction should apply between group 0 and 1
              $query->orCondition(array('d' => '1'), null, 1);  // OR conjunction should apply within condition group 1 only

                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: hwmaier at May 27, 2011, 06:52 PM

                But what about the use of the andCondition/orCondition calls which also provide the grouping parameter to achieve the same thing? Is andCondition/orCondition with grouping deprecated and should nested where be the preferred way? Shouldn’t below notation yield the same result as your embedded array syntax?
                $query->andCondition(array('a' => '1'));          // First group 0
                $query->andCondition(array('b' => '1'));          // 
                $query->andCondition(array('c' => '1'), null, 1); // AND Conjunction should apply between group 0 and 1
                $query->orCondition(array('d' => '1'), null, 1);  // OR conjunction should apply within condition group 1 only


                The grouping is a little funky and that lead to the nested array syntax being implemented. IOW, we ran into these issues with the grouping and decided to go another way. I did not deprecate the methods however, so I am totally open to improving/fixing them. FWIW, did you try assigning both sets to groups other than 0?