Changing Display Options in Model::find('list')

Posted on 05/19/2009 at 10:49 am by Kevin Wentworth
Viewed 13,920 times | 0 comments

I wanted to change the displayed field that Cake automatically returns with a Model::find('list').  If you have a database column named title or name (I'm not sure of the full list), CakePHP will automatically return an array index by =>  This usually works, but today I'm working with a database table that doesn't use that field- it's a table of uploaded files and uses the columns named 'src' or 'alt' to display information to the user.  I forgot how to do this, so I'm posting it for reference.

Changing the value of displayField

Model::displayField is the easiest way to change the automatically pulled db field.  You can change it in your model (for all requests), like so:

  1. $this->displayField = 'src';

Or, you can change it in your controller (for a single request):

  1. $this->Model->displayField = 'src';
  2. $docs = $this->Model->find('list);
  3. $this->set(compact('docs'));

Changing the Displayed Field by Options

The other way to change what field(s) you want regurned with the Model::find('list') query, is to use the $options array.  You don't have to include the 'id' field, but need to include the non-conventional fieldname you want displayed with Model:find('list') results.  You may even be able to pull a different field for your ID (not tested).

To change the display field through the $options variable, use the following code:

  1. $this->Model->find('list', array('fields'=>'src')); //This will return array('id'=>'src', 'id2'=>'src2');

Simple, but easy to forget! Cheers,

-Kevin Wentworth

Bookmark and Share

Tags for Changing Display Options in Model::find('list')

Cakephp | Database | Usage | Web Programming

Comments for this Posting

No comments. Be the first to post a reply.

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design