2014-10-01 14:35
浏览 18


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(
                '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 ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyao5533 2014-10-01 15:00

    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.

    点赞 打赏 评论
  • douhan8430 2014-10-01 20:31

    i've found something that solve my problem.

         'group' => 'Salarie.nom',
                        '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.

    点赞 打赏 评论

相关推荐 更多相似问题