baker
baker

Reputation: 21

CakePHP 2.x fields list shows list of id's instead of values

In my PartsController I have the following code:

$serialNrs = $this->Part->find('list',
        array(
        'conditions'=>array(
            'Part.status_id' => 3
        ),
        'fields'=> array('serial_nr_id' )
        //'fields'=> array('serialNr.name')
    ));

The according select field shows a list of id's instead of values. When I use 'fields'=> array('serialNr.name') I get an error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'SerialNr.name' in 'field list'

SELECT Part.serial_nr_id, SerialNr.name FROM part145.parts AS Part WHERE Part.status_id = 3

What do I need to do to show a list of values?

Upvotes: 0

Views: 576

Answers (1)

xinaris
xinaris

Reputation: 464

You should use the Model name instead of the actual table name in database, thus

$serialNrs = $this->Part->find('list',
    array(
    'conditions'=>array(
        'Part.status_id' => 3
    ),
    'fields'=> array('Part.name' )
));

According to the above code, you have a model named "Part" where it points to a database table where there are at least a "status_id" column and a "name" column.

Upvotes: 1

Related Questions