SELECT … AS … in CakePHP 1.2

This is a simple hint, but hopefully will be useful to some…

You might have noticed some examples where you have:

$this->Profile->find('all', array('fields'=>array('SUM(Profile.votes) AS total_votes')));

But what if you simply needed Profile.field AS another_name?
‘fields’=>array(‘Profile.field AS another_name’) … isn’t going to work, just like that.

What you need to do is escape the fields in your ‘fields’ array, like so:

$this->Profile->find('all', array('fields'=>array('`Profile`.`name` AS `another_name`')));

(Note the backticks)

%d bloggers like this: