Recent Posts
- (09/10) Fixing Warning: the ECDSA host key for 'github.com' differs from the key for the IP addressTAGS:Web Server Admin
- (12/26) CakePHP 3 - Getting List of Column Definitions from a Table (like schema())TAGS:CakephpCake3
- (09/14) Change Order of Loaded Behaviors in CakePHP 3TAGS:Cake3CakephpWeb ProgrammingPhp
- (05/29) CakePHP 3 - Accept JSON Header Only Working When Debug FalseTAGS:Web ProgrammingCakephpCake3
- (05/23) Remove All Events from Google Calendar (Reset Calendar)TAGS:Web ProgrammingPhp
- (11/08) Google Tag Manager (GTM) Not Firing Default PageView EventTAGS:Web ProgrammingJavascriptGoogle Tag Manager
- (10/13) In PHP, how do you get __toString() Magic Method Result without calling echo?TAGS:CakephpCake3Cakephp 13PhpWeb Programming
- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
Subscribe to my feed
MainelyDesign.com Blog
Changing Display Options in Model::find('list')
Posted on 05/19/2009 at 10:49 am by Kevin Wentworth
Viewed 14,312 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 Model.id => Model.name. 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:
- $this->displayField = 'src';
Or, you can change it in your controller (for a single request):
- $this->Model->displayField = 'src';
- $docs = $this->Model->find('list);
- $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:
- $this->Model->find('list', array('fields'=>'src')); //This will return array('id'=>'src', 'id2'=>'src2');
Simple, but easy to forget! Cheers,
-Kevin Wentworth
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!