WHERE- IN Clause in Editor

WHERE- IN Clause in Editor

davykiashdavykiash Posts: 35Questions: 13Answers: 1
edited July 2017 in Free community support

Hello,

How do you implement WHERE-IN statements in the server side code of the editor ?

This is what I have (Query in comments)

/*

SELECT id,orderno,ordertime,orderstatus FROM orders 
WHERE orderregister = 001
AND orderstatus IN (1,2,3)

*/


json_decode(Editor::inst( $db_details, $table, 'id')
    ->fields(
        Field::inst( 'id' ),
        Field::inst( 'orderno' ),
        Field::inst( 'ordertime' ),
        Field::inst( 'orderstatus' ),
        )   
                                                                                    
        ->where( 'orderregister', '001', '=' )                      
        ->where( 'orderstatus', (1,2,3), 'IN' )
    
        ->process($_POST)
        
        ->json(false))
    
);

Thanks.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin
    edited July 2017 Answer ✓

    Hi,

    The problem with that is that the (1,2,3) is actually "bound" as a string, so the database won't see it as an array. What we need to do is use an anonymous function which provides access to the Query class which has more options for controlling the conditions (docs).

    For example:

    ->where( 'orderregister', '001', '=' )
    ->where( function ( $q ) {
      $q->where( 'orderstatus', '(1,2,3)', 'IN', false );
    } )
    

    Note that although where is used in the anonymous function, $q is Query, not Editor, which is how this works.

    Allan

  • davykiashdavykiash Posts: 35Questions: 13Answers: 1

    Perfect!

This discussion has been closed.