Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 748
Shaheer Avatar asked Jun 02 '11 13:06

Shaheer


2 Answers

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).

like image 146
Dunhamzzz Avatar answered Nov 14 '22 08:11

Dunhamzzz


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.

like image 40
Stephen Avatar answered Nov 14 '22 06:11

Stephen