过滤来自关联的条件 [英] filter conditions from an association

查看:201
本文介绍了过滤来自关联的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索功能,它非常适合工作人员,所以我可以按名称搜索。
现在,我想通过staffgroup.groupname过滤员工,但unfortunatelly我得到这个错误:

 列未找到: 1054'where子句'中的未知列'staffgroups.groupname'

我有以下tablelayout




  • staffs(一个人可以属于多个组)

  • staff_staffgroups(HABTM链接表) / li>
  • staffgroups(有群组名称)






<我使用的条件如下:

  $ tmpConditions ['AND'] [] = array('Staff.isActive ='= >1); 
$ tmpConditions ['OR'] [] = array('Staff.lastname LIKE'=>%$ name%);
$ tmpConditions ['OR'] [] = array('staffgroups.groupname LIKE'=>%$ group%);
[...]

$ this-> Staff-> recursive = 1;
$ this-> paginate = array('conditions'=> $ tmpConditions);
$ this-> set('staffs',$ this-> paginate());

我无法设法让它工作,虽然我认为条件设置正确。

cheers endo

解决方案

到HABTM协会。你必须手动做。



首先你需要获取你正在寻找的StaffGroup.id

  $ group_ids = $ this-> Staff-> StaffGroup-> field('id',array('groupname LIKE'=>'%$ group%' )); 

然后取消绑定HABTM关联,然后将连接表绑定为hasMany关联

  $ this-> Staff-> UnbindModel(array('hasAndBelongsToMany'=> array('StaffGroup'))); 
$ this-> Staff-> bindModel(array('hasMany'=> array('StaffStaffGroup')));

您现在可以执行搜索

  $ this-> Staff-> StaffStaffGroup-> find(
'all',
array(
'conditions'=> array
'StaffStaffGroup.staff_group_id'=> $ group_ids,
'Staff.isActive ='="1,
'Staff.lastname LIKE'=>%$ name% ,


);


I have a search function, which works great for staff, so I can search by name. Now, I want filter staffs by staffgroup.groupname, but unfortunatelly i get this error:

Column not found: 1054 Unknown column 'staffgroups.groupname' in 'where clause'

I'm having the following tablelayout

  • staffs (a person can belong to many groups)
  • staff_staffgroups (HABTM-linking table)
  • staffgroups (has a groupname)

i used the conditions as follows:

$tmpConditions['AND'][] = array('Staff.isActive =' => "1");
$tmpConditions['OR'][] =  array('Staff.lastname LIKE' => "%$name%");
$tmpConditions['OR'][] = array('staffgroups.groupname LIKE' => "%$group%");
[...]

$this->Staff->recursive = 1;
$this->paginate = array('conditions' =>  $tmpConditions );
$this->set('staffs', $this->paginate());

I cant manage to get it working, allthough i think the Condition is set right.

cheers endo

解决方案

You cannot do that "out of the box", due to the HABTM association. You have to do it by hand.

First you need to get the StaffGroup.id(s) you are looking for

$group_ids = $this->Staff->StaffGroup->field('id', array('groupname LIKE' => '%$group%') );

Then unbind the HABTM association, and then bind the join table as hasMany association

$this->Staff->UnbindModel( array('hasAndBelongsToMany' => array('StaffGroup')) );
$this->Staff->bindModel(array('hasMany' => array('StaffStaffGroup')));

You can now perform your search

$this->Staff->StaffStaffGroup->find(
    'all', 
    array(
        'conditions' => array(
            'StaffStaffGroup.staff_group_id' => $group_ids,
            'Staff.isActive =' => "1",
            'Staff.lastname LIKE' => "%$name%",
        )
    )
);

这篇关于过滤来自关联的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆