Faulty where clause in Field::inst->options()

Faulty where clause in Field::inst->options()

loerezloerez Posts: 7Questions: 3Answers: 0

Hi,

i'm using a custom function to fill the select options of an editing bubble.
But whenever i add a where clause to the $db->select() method, i get this error: Column not found: 1054 Unknown column '0' in 'where clause'

The faulty code looks like this:

->options( function () use ( $db ) {
     $attrList = $db->select(
        'mytable',
        ['mytable.id as value, mytable.name as label'],
        ['mytable.deleted', '1', '!=' ] );
...

If i remove the where clause ,['mytable.deleted', '1', '!=' ] , it works like charm. I already tried different columns and different operators, but the error remains the same.

Any ideas what's wrong with my code?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,876Questions: 1Answers: 10,529 Site admin
    Answer ✓

    The array passed into where should be associative - e.g. [ 'mytable.deleted' => '1' ]. Of course that when things get a little bit more messy since we need a != which that form doesn't allow and thus a closure is required.

    The built in options() method does actually allow for what you have above:

    ->options( 'mytable', 'id', 'name', function ($q) {
      $q->where( 'deleted', '1', '!=' );
    } )
    

    Full details here.

    Do you do other logic in your custom method, or is that going to match what you need?

    Regards,
    Allan

  • loerezloerez Posts: 7Questions: 3Answers: 0

    Thank you, Allen! That's exactly what i was looking for.

This discussion has been closed.