Return selected fields within a cakePHP model find

Go To StackoverFlow.com

0

I simply want to return all rows from a users table but selected fields, and possible concat 2 fields.

normal sql could have just been:

SELECT id, CONCAT(`first_name`,`last_name`) AS `fullname` FROM `users`

But trying to achieve this in cake is a nightmare:

$users = $this->User->find("all") = returns everything
$users = $this->User->find("list",array("fields" => " ...) does not work either.

Can anyone please help me on how to use the cake model to achieve the simple query and return results

2012-04-04 08:19
by mauzilla
why not using virtual fields - mark 2012-04-04 08:50


7

You are close:

$this->User->find('all', array('fields' => array('CONCAT(first_name,last_name) AS fullname', 'otherfield'), 'contain' => array());
2012-04-04 08:21
by Andreas Wong
I guess I should have mentioned that users is linked to a variety of other tables, so doing the above returns one massive long array. Is there a way to isolate just searching the user table - mauzilla 2012-04-04 08:30
@MauritzSwanepoel yes, just pass in contain => false, edited my answe - Andreas Wong 2012-04-04 08:30
Ads