You Are Here: Home » Articles » Using afterFind() to create pseudofields in CakePHP

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

One useful application of this feature is in merging data from two or more database fields into one pseudofield. In the example below, 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.
  1. <?php
  2. class User extends AppModel
  3. {
  4.     var $name = 'User';
  5.     
  6.     function afterFind($results) {
  7.         // For any results returned from the 'User' model, take 'firstName' and 'lastName' and use them to produce a 'fullName' pseudofield.
  8.         foreach ($results as $key => $val) {
  9.             if (isset($val['User']['firstName']) && isset($val['User']['lastName'])) {
  10.                 $results[$key]['User']['fullName'] = $val['User']['firstName'] . ' ' . $val['User']['lastName'];  
  11.             }
  12.         }
  13.       return $results;
  14.     }
  15. }
  16. ?>

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:

  1. [User] => Array
  2.     (
  3.     [0] =>
  4.         (
  5.         [firstName] => Graeme
  6.         [lastName] => Garden
  7.         [fullName] => Graeme Garden
  8.         )
  9.         
  10.     [1] =>
  11.         (
  12.         [firstName] => Tim
  13.         [lastName] => Brooke-Taylor
  14.         [fullName] => Tim Brooke-Taylor
  15.         )
  16.     )

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.

  1. $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

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.

Leave a Comment

*
*
*

« Back to Articles

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

  • Stuntbox.
    David Sleight's Blog
  • hackdiary.
    Matt Biddulph's blog
  • A List Apart.
    Influential webzine for web designers
  • Cool Hunting.
    Finding things in the intersection of design, culture and technology that excite the imagination and inspire creativity

See More…

Back to top.