Recent Posts
- (02/24) Calling an Element from a Helper TAGS:CakephpWeb ProgrammingUsageTutorialExample
- (10/14) Forcing A Single Join in CakePHP PaginationTAGS:CakephpCakephp 13DatabaseErrorsHabtmMysqlMssqlWeb ProgrammingUsage
- (08/11) Inserting NOW() into MySQL Using CakePHPTAGS:CakephpDatabaseMysqlMssqlWeb ProgrammingUsage
- (08/07) Best PaginateCount for CakePHP - with Group By SupportTAGS:CakephpBehaviorsWeb ProgrammingUsageDatabaseExampleMysqlHabtmHack
- (04/03) CakePHP Error Messages Not Showing on FormTAGS:CakephpCakephp 13Web ProgrammingErrorsUsage
- (02/06) Sorting Paginated Results Using a Related Model Field in CakePHPTAGS:CakephpWeb ProgrammingUsageExample
- (11/02) Changing CakePHP's Model useTable on the FlyTAGS:CakephpWeb ProgrammingMysqlDatabaseExampleTutorial
- (10/18) The Funniest Error Message Ever - Thank You EclipseTAGS:EclipseWeb ProgrammingErrorsHumorWindowsUsageSoftwarePhpCakephp
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 1,715 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!
