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
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).
When in doubt, read the manual:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With