(This is mostly a summary of Dealing with calculated fields in CakePHP’s find().)
One of the great things about CakePHP is that if it doesn’t have some core functionality you want/need there are easy ways to add it. More and more I’m taking advantage of this ability. This all comes about because I wanted to have calculated fields available inline with the model data. By calculated fields I mean results that are not data columns (e.g. SELECT *, CURDATE() AS current_date FROM users
… yes, that’s a fairly contrived example).
By default Cake places results from calculated fields outside the model data, like this:
Array ( [0] => Array ( [User] => Array ( [id] => 1 [username] => aaas ) [0] => Array ( [current_date] => '2011-05-13' ) ) )
What we want is to place the “current_date” calculated field inside the User model, so it’s more naturally accessed with $users[0]['User']['current_date']
instead of $users[0]['User'][0]['current_date']
. Easy enough to do through a model’s afterFind()
callback method (to make it widely available place the function in app_model.php).
function afterFind($results, $primary=false) { if($primary == true) { if(Set::check($results, '0.0') && Set::check($results, "0." . $this->alias)) { $fields = array_keys( $results[0][0] ); foreach($results as $key=>$value) { foreach( $fields as $fieldName ) { $results[$key][$this->alias][$fieldName] = $value[0][$fieldName]; } unset($results[$key][0]); } } } return $results; }
And yet, even as I add such enhanced functionality to my web apps I’m finding limits. The above logic is complicated a little because you don’t know what type of results you’re getting. The results you get from calling, for example, find('all')
versus find('count')
are not the same. But Cake doesn’t give any hints to the afterFind()
callback, and as a result additional logic is included to try and guess at our data structure. The above adds a quick but incomplete hack by a) checking that the results are for the primary model queried (e.g. not from contained model), b) checking for the presence of nested numerical keys, and c) checking that there is model data to integrate with.
The take away is that while the code produces the desired data structure, in its current form it does so only for specific results.