I am mikek's Blog

The personal blog of Mike Karthauser, who you may know from Brightstorm Limited or the internet.

CakePHP virtual Fields

Something pretty useful in the CakePHP right now is VirtualFields.

A virtual field is a way to combine several fields to make a new ‘virtual’ one that exists when you read from the model. So with ‘firstname’ and ‘surname’ you can make a ‘full_name’ field which will allow you to use this within a find(‘list’) – a notoriously tricky task otherwise.

To do this on the User Model for example, just add the following to your app/models/user.php.

var $virtualFields = array(
'full_name' => 'CONCAT(User.firstname, " ", User.surname)'
);

Simple and powerful.

Available in CakePHP 1.3.*

Filed under: code snippets, , ,

what's hot

Flickr Photos

Sponsor Mike run the Bath Half marathon

mikek on twitter