Shaheer
Shaheer

Reputation: 2147

cakephp select list showing ids instead of text

i am using cakephp 1.3 to generate a form i am creating a select list using hasOne and belongsTo relation

my models: image, category

category hasMany image

image belongsTo Category

category table has two columns id and category_name

i am doing

$this->set('categories', 
           $this->Image->Category->find(
                                        'list',
                                         array( 'order' => 'category_name ASC' )
                                       )
          ); //to generate the select list

so far so good, there is only one problem left, the select list generated shows the id of the category instead of the category_name as the option text, i know this is not cakePHP's fault but i need to know the solution

any help please.

P.S if i am unclear about the question please let me know

Upvotes: 5

Views: 4220

Answers (2)

Dunhamzzz
Dunhamzzz

Reputation: 14798

You need to define the displayField property in your category model, so that CakePHP can properly determine which field to display as a label. This code in your category model will fix it for you:

var $displayField = 'category_name';

Alternatively, rename the category_name field to 'name' or 'title' (I would do this, it's obvious that a 'name' field in a category table is going to be the name of the category).

Upvotes: 6

Stephen
Stephen

Reputation: 18964

When in doubt, read the manual:

3.7.3.1.4 find('list')

When calling find('list') the fields passed are used to determine what should be used as the array key, value and optionally what to group the results by. By default the primary key for the model is used for the key, and the display field (which can be configured using the model attribute displayField) is used for the value.

Upvotes: 2

Related Questions