Use of custom displayField paramater in CakePHP

The $this->displayField is used to define custom display field while doing ‘list’ or scaffolding in CakePHP. It can be specified either in Model class or just before making a db call.

For exmple, assume that you have two fields i.e. “id” and “username” in your “users” table. While making a list call – $this->User->find(‘list’) – as normal, the result returned would be:

array(
1=>1,
2=>2,
3=>3
);

because, by default CakePHP uses “title” or “name” field to be shown as “displayField” but as here none of them was present, it regarded “id” field as “displayField”.

So to make “username” appear as “displayField” do one of things following. Either place it in the Model class “User” like:

class User extends AppModel {
	var $displayField = "username";
}

Or just before making the call::

$this->User->displayField = 'username';
$this->User->find('list');

Now the list result would be something like:

array(
1=>”arvindk”,
2=>”rakeshg”,
3=>”vishalk”
);

Leave a Reply

Your email address will not be published.