Editor: Adding where clause to server side script for a Field Select Type

Editor: Adding where clause to server side script for a Field Select Type

johnhpejohnhpe Posts: 12Questions: 3Answers: 0
edited July 2019 in Free community support

HI, having problems is the Editor Form displaying the correct data in a select (dropdown). The documentation at https://editor.datatables.net/docs/1.8.0/php/class-DataTables.Editor.Options.html states there is a where clause available but whenever I add it it returns an empty list. Without the where clause I get all entries.
What am I doing wrong I have tried the following as well as ->where ('to_type')

Field::inst( 'to_ports.switch_id')
                ->options( Options::inst()
                ->table( 'switches' )
                ->value( 'id' )
                ->label( 'name' )
                ->where( 'to_type', '=','1' )
            )
            ->set(false)

Interestingly the debug output gives this query which is obviously wrong as the 'to_type' should be NOT NULL

query: "SELECT DISTINCT  `id` as 'id', `name` as 'name' FROM  `switches` WHERE `to_type` IS NULL "

Documentation does not give any examples for this that I can see, any help appreciated. Also no errors thrown anywhere

This question has an accepted answers - jump to answer

Answers

  • johnhpejohnhpe Posts: 12Questions: 3Answers: 0

    Any ideas from anyone on this? I think it may be a bug as I have looked through the code and cannot see how the where clause is computed

  • allanallan Posts: 63,175Questions: 1Answers: 10,409 Site admin
    Answer ✓

    the 'to_type' should be NOT NULL

    If that's the condition you want, use:

    ->where( function ($q) {
      $q->where( 'to_type', null, '!=' );
    } )
    

    Regards,
    Allan

  • johnhpejohnhpe Posts: 12Questions: 3Answers: 0

    Thanks Allan, that did the trick, I didn't realise I had to call a function....

This discussion has been closed.