I am in a Task model.
I build following options to load the Task and associated User model data.
[php]$options = array(
‘conditions’ => $conditions,
‘contain’=>array(‘User’=>array(‘id’, ‘full_name’))
);[/php]
Remember that User.full_name is a field created in afterFind callback of User model using the famous “_attachAttributes” function or approach. All works fine.
As it gives me all 30 odd fields of Task model I wanted to limit number of fields to have only Task.id and Task.task_name fields. In order to do it I try this:
[php]$options = array(
‘conditions’ => $conditions,
‘fields’ => array(‘Task.task_name’, ‘Task.id’),
‘contain’=>array(‘User’=>array(‘id’, ‘full_name’))
);[/php]
But it gave this error:
[text]Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘User.full_name’ in ‘field list’
SQL Query: SELECT `Task`.`task_name`, `Task`.`id`, `User`.`id`, `User`.`full_name` FROM `thecontrolistdb`.`tasks` AS `Task` LEFT JOIN `thecontrolistdb`.`users` AS `User` ON (`Task`.`user_id` = `User`.`id`) WHERE `Task`.`due_date` >= ‘2015-06-01’ AND `Task`.`due_date` <= ‘2015-06-30’ AND `Task`.`deleted` = ‘0’[/text]
That’s probably because the later would generate and do query with the help of fields passed in “fields” option. While in former it would fetch all the Task records then fetch associated entries, i.e. of User model and implemented the afterFind data of User model.
As a work around so that the number of Task fields are limited and we do have a full_name as well, I do the following:
[php]$options = array(
‘conditions’ => $conditions,
‘fields’ => array(‘Task.task_name’, ‘Task.id’),
‘contain’=>array(‘User’=>array(‘id’, ‘first_name’, ‘last_name’))
);[/php]
It gives me limited Task fields and also create the Task.full_name as it is already there in the afterFind callback.