Using afterFind() to create pseudofields in CakePHP
CakePHP provides a useful model function, afterFind(), which can be used to manipulate results returned from any find(), findAll() or findBy<field>() query.
Tagged with CakePHP and Web Development
Posted on 6/5/07 by Paul Herron
afterFind() is added to a User model. A foreach statement takes the firstName and lastName fields for each user and creates a new field, fullName.
- <?php
- class User extends AppModel
- {
- var $name = 'User';
- function afterFind($results) {
- // For any results returned from the 'User' model, take 'firstName' and 'lastName' and use them to produce a 'fullName' pseudofield.
- foreach ($results as $key => $val) {
- $results[$key]['User']['fullName'] = $val['User']['firstName'] . ' ' . $val['User']['lastName'];
- }
- }
- return $results;
- }
- }
- ?>
CakePHP makes $results available automatically, and automatically deals with them when they're returned from the afterFind(), so this function should work 'as is'.
All being well, a find(), findAll() or findBy query in the controller will now give an array structure like this:
- [User] => Array
- (
- [0] =>
- (
- [firstName] => Graeme
- [lastName] => Garden
- [fullName] => Graeme Garden
- )
- [1] =>
- (
- [firstName] => Tim
- [lastName] => Brooke-Taylor
- [fullName] => Tim Brooke-Taylor
- )
- )
afterFind() can result in significantly tidier code, and puts this kind of processing where it belongs - in the model. It's also a useful workaround for inserting more than one field into a generateList() statement. The label parameter in generateList() can only be sourced from one field, but this can be the pseudofield generated in the example above.
- $this->set('users', $this->User->generateList(null,null,null,'{n}.User.id','{n}.User.fullName'));
This would set a $users array for use in the view, and could be used be used to output a select box with each option set to a user's full name.
Comments
Leave a Comment
Article Tags
Show all articles, or just those tagged as:
Apache (1)
CakePHP (5)
Domains (1)
Ethics and That (1)
Freeware (1)
Open Source (1)
Servage (1)
SMS (1)
Software (1)
WAMP (1)
Web Development (6)
Windows (2)
Feed
The articles RSS feed is available here.
Elsewhom
-
mattheaton.com.
Bluehost Blog
Matt is CEO of Bluehost, a successful web hosting provider -
Snook.ca.
Home of web developer Jonathan Snook -
RichardHerring.com.
Home of the comedian and online journal-keeper -
Jeffrey Zeldman Presents the Daily Report.
Web design, news and info since 1995

Tane Piper wrote on 18/7/07:
Hey - great article! Works a treat in my application for generateList and having more than one value!Phil McGuire wrote on 31/7/07:
Thanks for the article! Just what I needed but there's a syntax error in it just so you know.
if (isset($val['User']['firstName']) && (isset($val['User']['lastName'])) {
should be:
if (isset($val['User']['firstName']) && isset($val['User']['lastName'])) {
Thanks!
Paul Herron wrote on 25/8/07:
Hi Phil,
Glad you found it useful!
Thanks for the info on the mistake - that's amended now.
Mike wrote on 24/6/08:
Hey.
Nice tip, I was struggling to find out how to do this for a while, but it works like a charm.
Thank, Mike.