You are Here: 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.

zoli wrote on 25/10/08:

Thanks, very useful article!

 Wouldn't need such inventions if CakePHP would be a true OOP framework, but... c'est la vie. I suggest using Ruby on Rails instead.

Bennigraf wrote on 3/11/08:

Hi there!

Thanks for your hint - I came across by chance and it helped me alot already, although it's kind of old. I just discovered one problem: When only one result is returned (both on purpose using find('first') and just using conditions) cake creates the $results-array without the numeric indizes for which your way doesn't sets the fullname, since $key is already 'User' and val contains the indizes 'firstName' and 'lastName' directly.

 Any Ideas on how to work around this except adding another if (like if(isset($var['firstName'] && ...))?

Greetings, Benni. 

Nate Klaiber wrote on 7/11/08:

I would agree with zoli. This is a lot of extra work to get a 'full_name' method. If CakePHP would be truly OOP, then I could have an instance method - and I wouldn't need any extra loops to iterate over and add such methods. I just can't see how this is a good design decision.

dogmatic wrote on 29/4/09:

@Nate

if you think its not so good why not supply a better solution then?

Heryudi Ganesha wrote on 28/7/09:

Hi, thanks for the tips! But when I'm using it like, example:

 

if ($val['Posting']['amount'] == 1) {

 

This gives me following notice:

Undefined index:  Posting [APP\models\posting.php, line 38]

Line 38: if ($val['Posting']['amount'] == 1) {

 

The logic in the afterFind callback works perfectly the way it's supposed to do.

 

Any idea why cakephp display the notice?

Atea Webdevelopment wrote on 12/4/10:

Any clues on what I need to do to sort on a pseudofield?

 

Should I add a custom Mysql query?

I need to sort on someting like this:

COALESCE(price_month_action, price_month_regular) AS price_month_least

 

 

Joel Bradshaw wrote on 10/11/10:

Awesome!  This is exactly what I figured should be easy with Cake, and it's short, sweet, and perfect.  Thanks!

zyx wrote on 24/8/11:

Great! very useful.

office2010key wrote on 16/11/11:

The Microsoft office 2011 users can Office 2010 work more efficiently with the Microsoft Office 2010 simple and intuitive Download Office 2010 user interface new graphical MS Office 2010 menus and a familiar Office 2010 Key experience provide the Office 2010 Download tools you need to plan and Office 2010 Professional manage your project. And the Microsoft Office 2010 Download tabs and contextual menu buy microsoft office 2010 groupings reveal powerful features, Windows 7 making it easier to get Microsoft Windows 7 started and realize results Win 7. Simply be more productive Download Windows 7 by choosing your own menu Buy Windows 7 options, quick access Office 2007 toolbar settings and Microsoft Office 2007 setting defaults.Enhanced copy Office 2007 Download and paste makes it easy to Office 2007 Professional share information effectively MS Office 2007 with other Office 2007 Key applications while retaining buy microsoft office 2007 formatting. And the added columns Download Office 2007 dynamically by entering Microsoft Office 2007 Professional data without having to first select purchase microsoft office 2007 the data type.

http://softwarestockss.blogspot.com

http://software-stocks.com

5345 wrote on 2/1/12:

Loving the pastels louis vuitton sale for some cause LV clothes normally look greater within the advertisements than even on the runways or red carpet. But for positive this puts me in the mood for some spring time warmth so hurry up and be more than winter louis vuitton speedy mini!I under no circumstances substantially cared for the louis vuitton speedy 25 or this style but I actually really like this bag! I think I was just constantly turned off because these sort of bags look hard to obtain louis vuitton sale into when you are on the go to me?- deciding upon an high priced gift out of thin air may be a daunting process. We are right here to create it just somewhat bit simpler for you with our Holiday louis vuitton speedy 30, and today's installment will concentrate on the safest kind of accessory gift: The Classic Bag.On the other hand, for the fun of it, I just ran this past my husband, who is an lawyer. He opines that Hermes would have grounds to sue simply because one more maker has intentions of turning a profit depending on an louis vuitton speedy'patented style style. In any occasion, I believe these canvas bags are dorky.Appreciate the Louis V and also the retailers have the most beneficial windows of any individual on a continuous basis. Somehow I in no way really like the louis vuitton speedy sale lines, but the LV is superb.What cheap louis vuitton has employed appears to be a lot more of an artistic rendering than a straight-up photo like that applied by louis vuitton luggage Thursday Friday, but apart from that, the idea is exactly the same and I'm betting that Hermes'legal department will react similarlylouis vuitton neverfull and eventually get a comparable settlement.if Hermes is going to invest its income on a court case suing a firm for copying its bags, then speedy louis vuitton should really leave the gently humorous canvas bags alone and go soon after chain retail stores that sell carbon-copy versions of its bags instead.

Leave a Comment

CAPTCHA[Refresh]

« Articles

Article Tags

Show all articles, or just those tagged as:

Feed

The articles RSS feed is available.

Elsewhom

See More…

Back to top.