Need help understanding the Editor ->where() statement

Need help understanding the Editor ->where() statement

ken@kmplace.comken@kmplace.com Posts: 5Questions: 4Answers: 0
edited April 2020 in Free community support

I'm fairly new with DataTables/Editor and need to understand how to get the 'where' configured in Editor. My SQL statement is conditioned on 1 of 3 variable passed in thru $_COOKIES.

My code:

$territory = $_COOKIE['territory'];
$category = $_COOKIE['category'];
$status = $_COOKIE['status'];

Editor::inst( $db, 'contacts', 'con_id' )
    ->fields(
        Field::inst( 'territory' ),
        Field::inst( 'postal' ),
        Field::inst( 'house' ),
        Field::inst( 'street' ),
        Field::inst( 'first_name' ),
        Field::inst( 'last_name' ),
        Field::inst( 'phone' ),
        Field::inst( 'mobile' ),
        Field::inst( 'email' )
    )           
    ->where('territory', $territory)                   // $territory have a non-null value
    ->where('category', $category)              // $category have a non-null value
    ->where('status', $status)                      // $status have a non-null value
    ->process( $_POST )
    ->json();

How could I declare the ->where() statements dynamically so that they are only included if the specified value is non-null?
For example I may only want the first one and the third one.

Thanks in advance

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,144Questions: 1Answers: 2,586
    Answer ✓

    I believe you can group those conditions, see here.

    So something like:

    $editor->where( function ( $q ) {
        $q
            ->where( 'territory', null, '!=' )
            ->and_where( function ( $r ) {
                $r->where( 'territory', $territory);
            } );
    } );
    

    Colin

This discussion has been closed.