Sunday, November 22, 2009

Field Name Conflicts - Consider using displayField

displayField
If your table has two fields, i.e., 'name and 'title', you need to specify

     var $displayField = 'name';

in the Model.

Example: 
In my Category table I used both 'name' and 'title' as names of two different fields. Then in my Category model (file:// 'app/models/category.php), I used the following statement to tell Cake to use 'name' as label.

<?php
class Category extends AppModel {
 var $name = 'Category';
 var $displayField = 'name';
}
?>

The statement var $displayField = 'name' forces Cake to use 'name' as the label. As such, you can choose any field name to use as a label in this way.

CakePHP makes a label from either 'name' or 'title' field. When both are present in your table, the system may get confused. I am not sure if this is the general rule, but, this is how I fixed my problem.

Hope this helps someone.

No comments: