how to filter select fields options ?

how to filter select fields options ?

LapointeLapointe Posts: 430Questions: 81Answers: 4

I try to set where clause when getting option list, and try a lot but nothing run

            Editor::inst( $db, $TblName )
                ->fields(
                    Field::inst( 'documents.Operation' )
                        ->options( Options::inst()
                            ->table( 'operations' )
                                ->value( 'ID' )
                                ->label('Libelle')
// not run
//                      -> where('ID',$F)

// $F is set at start and this part (global select) is ok       
//              ->where('documents.Operation',($F)?$F:0,'>')            
//              ->process( $_POST )
//              ->json();

// not run - $F is set at start but not visible for the select field filter function 
                        ->where( function($q, $F) {
                                    $q ->where( function($r) {
                                        $r ->where('ID',($F)?$F:0,'>');
                                    });
                                } )   

Does somebody do that please, or an idea?
Thanks by advance

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin
    Answer ✓

    You need to do this:

    >where( function($q) use ($F) {
    

    That's how you access externally scoped variables in PHP anonymous functions. See the PHP docs for full details.

    Allan

  • LapointeLapointe Posts: 430Questions: 81Answers: 4

    Thanks a lot again
    in fact there were 2 mistake in my code (use () and ? : choice... ever the > operator was set

    // wrong
                            ->where( function($q, $F) {
                                        $q ->where( function($r) {
                                            $r ->where('ID',($F)?$F:0,'>');
                                        });
                                    } )  
    // correct
                  ->where( function($q) use ($F) {
                        $q ->where('ID',($F)?$F:0,($F)?'=':'>');
                 } )
    
    

    B)

This discussion has been closed.