My $condition in find() cannot make use of virtualfield.
I have this virtualfield on my Employee model:
var $virtualFields = array(
'AgentFullName' => "CONCAT(Employee.id, ' ', Employee.emp_ape_pat, ' ', Employee.emp_ape_mat, ' ', Employee.name)"
);
I’m using this condition to look on two fields, one regular, one virtual (
$idconditions = array(
'OR' => array(
'Employee.id LIKE' => $this->passedArgs['valsearch'],
'Employee.AgentFullName LIKE' => $this->passedArgs['valsearch']
));
My find returns no record when looking for a valid text that can be found on the virtual field:
$theid = $this->Horario->Employee->find('first', array(
'fields' => array('Employee.emp_appserial', 'Employee.AgentFullName'),
'conditions' => $idconditions,
));
However, the sql_dump seems correct:
When find() looks for an id number:
SELECT `Employee`.`emp_appserial`, (CONCAT(`Employee`.`id`, ' ', `Employee`.`emp_ape_pat`, ' ', `Employee`.`emp_ape_mat`, ' ', `Employee`.`name`)) AS `Employee__AgentFullName` FROM `devopm0_5`.`employees` AS `Employee` WHERE ((`Employee`.`id` LIKE 1005) OR ((CONCAT(`Employee`.`id`, ' ', `Employee`.`emp_ape_pat`, ' ', `Employee`.`emp_ape_mat`, ' ', `Employee`.`name`)) LIKE '1005')) LIMIT 1
And the find() using last name (returns no record)
SELECT `Employee`.`emp_appserial`, (CONCAT(`Employee`.`id`, ' ', `Employee`.`emp_ape_pat`, ' ', `Employee`.`emp_ape_mat`, ' ', `Employee`.`name`)) AS `Employee__AgentFullName` FROM `devopm0_5`.`employees` AS `Employee` WHERE ((`Employee`.`id` LIKE 'SMITH') OR ((CONCAT(`Employee`.`id`, ' ', `Employee`.`emp_ape_pat`, ' ', `Employee`.`emp_ape_mat`, ' ', `Employee`.`name`)) LIKE 'SMITH')) LIMIT 1
I wonder why the virtualfield cannot be subject to search.
Can you help?
Thank a lot !
You cannot use VirtualFields as SQL Conditions.
The virtual fields are added to the results after the find is performed, and this is why you cannot condition on them.
The Cookbook says: