Hello. I want to receive model data by find(all), but the user should get only a restricted set of table fields. That's easy:
$ret = $this->find('all',array('fields'=>array(
'Employee.id','Employee.address_id'
)));
But this model (Employees model) also has a belongsTo association:
var $belongsTo = array(
'Address' => array(
'className' => 'Address',
'foreignKey' => 'address_id',
'fields' => array('Address.full_name')
)
);
I want the Address.full_name
field to appear in my fetched data too. But it doesn't work with the find() call above, and it throws an error (SQL Error: 1054: Unknown column 'Address.full_name' in 'field list') when trying this:
'fields'=>array('Employee.id','Employee.address_id','Address.full_name')
Anyone knows how to solve this?
EDIT: I totally forgot that Address.full_name is a virtual field. Looking at the Cakephp-produced SQL, it's obvious why it doesn't work:
SELECT
`Employee`.`id`, `Employee`.`address_id`, `Address`.`full_name`
FROM
`employees` AS `Employee`
LEFT JOIN `addresses` AS `Address`
ON (`Employee`.`address_id` = `Address`.`id`)
WHERE 1 = 1
In the address model, full_name is defined like this:
var $virtualFields = array(
'full_name' => 'CONCAT_WS(" ", Address.firstname, Address.surname)'
);
So then, the question is: Is it a CakePHP bug that it's not able to include (foreign model's) virtual fields within a fieldlist supplied to find()?