drux41001 2014-10-01 14:35
浏览 18
已采纳

CakePhp:查找/分页“hasMany”相关模型的条件

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 ?

  • 写回答

2条回答 默认 最新

  • 普通网友 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能