i'm trying to make a litle search action for one of my Model. My Model Salarie
was associated with some other models, in particular :
Salarie
-> hasMany -> Permission
Salarie
-> hasOne -> Carburant
So i try to make a find() on my Model Salarie
to select records that match a keyword. This keyword can by found in my model Salarie
, or in associated model Permission
, Carburant
.
Here is my actual find() :
$salaries = $this->Salarie->find('all',array(
'contain' => array(
'User','Agence','Entitee','Fonction','Contact','Ligne','Car','CallingCard','Badge','Carburant','Permission'),
'conditions'=>array(
'OR'=>array(
'Salarie.nom LIKE' => '%'.$keyWord.'%',
'Salarie.prenom LIKE' => '%'.$keyWord.'%',
'Salarie.secu LIKE' => '%'.$keyWord.'%',
'Salarie.tel LIKE' => '%'.$keyWord.'%',
'Salarie.mobile LIKE' => '%'.$keyWord.'%',
'Salarie.ville LIKE' => '%'.$keyWord.'%',
'Salarie.email LIKE' => '%'.$keyWord.'%',
'Carburant.numero LIKE' => '%'.$keyWord.'%',
)
)
));
This code works but doesn't looks into Permission
table/model, if i add this line 'Permission.login LIKE' => '%'.$keyWord.'%',
in my conditions array i got a error
Column not found: 1054 Unknown column 'Permission.login' in 'where clause'
I've try with contain
but it's not what i looking for, contain
filters datas into the results of the find
.
if someone got a tips ?
SQL joins are only done by hasOne and belongsTo associations, not for hasMany and HABTM. You can use Linkable plugin to filter records based on conditions for hasMany associations.
i've found something that solve my problem.
'group' => 'Salarie.nom',
'joins'=>array(
array(
'table'=>'permissions',
'alias'=>'Permission',
'type'=>'LEFT',
'conditions'=>array(
'Salarie.id = Permission.salarie_id',
)
)
)
if i had this code to my find the SQL query works great. I just have to GROUP BY
to avoid double result.