Where condition for editor select list populated from left join

Where condition for editor select list populated from left join

peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0
edited July 2021 in Editor

Trying to filter options in an editor select list.

The list is populated from table unit_group though a left join.

I want to only add options from that table where unit_group.type == 'learning_event'.

I have tried with an AJAX error. Checked network -> XHR but there is no response.

Editor::inst( $db_cm_dopt, 'learning_event', 'learning_event_pk' )    
    ->field(
        Field::inst( 'learning_event.learning_event_name' ),
    Field::inst( 'learning_event.type' ),
        Field::inst( 'learning_event.modified' ),
        Field::inst( 'learning_event.modified_by' )->setValue( $user),
      Field::inst( 'learning_event.unit_group_fk' )
            ->options( Options::inst()
                ->table( 'unit_group' )
                ->value( 'unit_group_pk' )
                ->label( 'unit_group' )
                >where( function ($q) {
            $q->where( 'unit_group.type', 'learning_event%', 'LIKE' );
                }
            ),
        Field::inst( 'unit_group.unit_group' )
    )
     ->leftJoin( 'unit_group', 'unit_group.unit_group_pk', '=', 'learning_event.unit_group_fk' )

This question has an accepted answers - jump to answer

Answers

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0

    There is also a fullext index on the type column, though I suspect that is not the issue. Also, I would prefer to have a == rather than LIKE.

  • peterbrownepeterbrowne Posts: 314Questions: 54Answers: 0
    edited July 2021

    That should be:

    Editor::inst( $db_cm_dopt, 'learning_event', 'learning_event_pk' )    
        ->field(
            Field::inst( 'learning_event.learning_event_name' ),
        Field::inst( 'learning_event.type' ),
            Field::inst( 'learning_event.modified' ),
            Field::inst( 'learning_event.modified_by' )->setValue( $user),
          Field::inst( 'learning_event.unit_group_fk' )
                ->options( Options::inst()
                    ->table( 'unit_group' )
                    ->value( 'unit_group_pk' )
                    ->label( 'unit_group' )
                    >where( function ($q) {
                $q->where( 'unit_group.type', 'learning_event' );
                    })
                ),
            Field::inst( 'unit_group.unit_group' )
        )
         ->leftJoin( 'unit_group', 'unit_group.unit_group_pk', '=', 'learning_event.unit_group_fk' )
    

    now works :-)

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Glad you got it sorted!

    Colin

Sign In or Register to comment.