I am using CakePHP 2.0 and I have a Model with this ‘virtualFields’:
Country.php:
var $virtualFields = array(
'path' => "CONCAT_WS('/', dirname, basename)"
);
If I do this in a Controller which uses “User”
$users = $this->User->find('all');
the virtual field path is set.
If I use this in my Controller, which also uses “User”
$options['fields'] = array(
'DISTINCT User.*'
);
$options['joins'] = array(
array(
'table' => 'courses',
'type' => 'inner',
'conditions' => array(
'User.id = courses.user_id'
)
),
array(
'table' => 'times',
'type' => 'inner',
'conditions' => array(
'courses.id = times.course_id'
)
)
);
$options['conditions'] = array(
'times.amount > ' => 0
);
$users = $this->User->find('all', $options);
With the options, the path field is not set, of course, the SQL query does not seem to have the “CONCAT_WS(‘/’, dirname, basename)” field included, which is included if I do the find operation without the options.
What can I do having the options, so that the virtual field is automatically included? Of course I can write in the fields options the CONCAT, but that’s not very nice, especially if I change it.
Best regards.
Use group by rather than distinct and remove the fields option completely.