Nested AND and OR expressions in Editor PHP libraries

Nested AND and OR expressions in Editor PHP libraries

epep Posts: 24Questions: 6Answers: 0

Hello,

I studied https://editor.datatables.net/manual/php/conditions , but I could not find how to express an sql query like in the following snippet:

... WHERE age > 18 AND ( name = 'Allan' OR name = 'Peter' ) AND location = 'Edinburgh'

using the Editor PHP libraries. Could you please help me.

Thanks,
Eberhard

Answers

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394
    edited August 2019

    Look at "Multiple conditions" and "Simple OR condition" here:
    https://editor.datatables.net/manual/php/conditions#Or-operators-and-grouping

  • epep Posts: 24Questions: 6Answers: 0

    Thank you. Following your comment I would suggest

    $editor->where( 'age', '18', '>' )
      ->where( function ( $q ) {
          $q->where( function ( $r ) {
            $r->or_where( 'name', 'Allan' );
            $r->or_where( 'name', Peter' );
          });
        })
      -> where( 'location', 'Edinburgh' );
    

    But this doesn't seem to work.

    Thanks,
    Eberhard

  • epep Posts: 24Questions: 6Answers: 0

    For those who might be interested: I finally found a solution. In fact my values are not static but stored in variables. So I have to use "use" for the global variables:

    $editor->where( function( $q ) use( $col, $word1, $word2 ){
      $q->where( $col, '%' . $word1 . '%', 'LIKE' ); 
      $q->or_where( $col, '%' . $word2 . '%', 'LIKE' );
    });
    ...
    
This discussion has been closed.