how i can use where function in the select options

how i can use where function in the select options

saee2838saee2838 Posts: 18Questions: 6Answers: 0
 Editor::inst( $db, 'users_visits', array('event_id', 'event_level') )
    ->where( 'event_id', $eventid )
    ->debug( true )
    ->field( 
        Field::inst( 'users_visits.event_id' )
            ->options( Options::inst()
                ->table( 'sport_calendar_match_events' )
                ->where('id',$event_id)               <<<<<<<<<<<<<<<<<<<<<<<-------------------------- do not work       
                ->value( 'id' )
                ->label( array('name') )
            )
            ->validator( 'Validate::dbValues' ),
        Field::inst( 'users_visits.level_name' )->validator( 'Validate::notEmpty' ),

        Field::inst( 'users_visits.competition_type' ),
        Field::inst( 'sport_calendar_match_events.name' )
            ->set( false )
    )


This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin

    That should be all you need to do. I would point out on line 8 you have $event_id while on line 2 you have $eventid. Is that intentional?

    Allan

  • saee2838saee2838 Posts: 18Questions: 6Answers: 0

    i change it
    but it dosent work
    when i add ->where( 'id' , $eventid )
    it dosent work

  • saee2838saee2838 Posts: 18Questions: 6Answers: 0

    i test ->where( 'id' , 1)
    but not working

  • saee2838saee2838 Posts: 18Questions: 6Answers: 0

    i try with compoundKey example in source
    but it dose not work

  • saee2838saee2838 Posts: 18Questions: 6Answers: 0

    when i use this code

    $userid=5;
    // The key thing to note for compound key support is the use of an array as the
    // third parameter for the Editor constructor, which is used to tell Editor what
    // the primary key column(s) are called (default is just `id`).
    Editor::inst( $db, 'users_visits', array('user_id', 'visit_date') )
        ->debug( true )
        ->where('user_id', $userid)
        ->field( 
            Field::inst( 'users_visits.user_id' )
                ->options( Options::inst()
                    ->table( 'users' )
                    ->value( 'id' )
                    ->label( array('first_name', 'last_name') )
                    ->where( function ($db) {
                        $db->where( 'id', 5 );     <<<<<<<<<<<<<-----------------here
                    })
                )
                ->validator( 'Validate::dbValues' ),
    

    its work
    but when i use $userid in second where it dosent work

    $userid=5;
    // The key thing to note for compound key support is the use of an array as the
    // third parameter for the Editor constructor, which is used to tell Editor what
    // the primary key column(s) are called (default is just `id`).
    Editor::inst( $db, 'users_visits', array('user_id', 'visit_date') )
        ->debug( true )
        ->where('user_id', $userid)
        ->field( 
            Field::inst( 'users_visits.user_id' )
                ->options( Options::inst()
                    ->table( 'users' )
                    ->value( 'id' )
                    ->label( array('first_name', 'last_name') )
                    ->where( function ($db) {
                        $db->where( 'id', $userid );    <<<<<<<<---------here
                    })
                )
                ->validator( 'Validate::dbValues' ),
    
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Answer ✓

    That is correct and expected as that's how PHP closures work. You need to use the use statement:

    ->where( function ($db) use ($userid) {
    

    See the PHP documentation on anonymous functions for more details.

    Allan

This discussion has been closed.