过滤HABTM相关模型的条件 [英] filter HABTM-associated model with conditions

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

问题描述

序言:几天前,我问了一个问题来解决HABTM-过滤器,我不能这样做,即使与教程,所以Obi Kwan Kenobi你是我唯一的希望。



我想实现:过滤StaffStaffgroup中使用的GroupID的员工



我有以下tablelayout




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

  • staff_staffgroups(HABTM链接表)

  • staffgroups )



变量$ tmp获取一个工作数组,但问题是Staff是StaffStaffgroups的子对象。我可以解析throu和重新组装数组,但这不是一个很好的解决方案。
所以我想使用工作人员条件(见换行),但是我得到错误1054列未找到:1054未知列。我尝试绑定和解除绑定,但没有结果。

  $ group_id = 2; 
$ tmp = $ this-> Staff-> StaffStaffgroup-> find('all',
array('conditions'=> array(
'StaffStaffgroup.staffgroup_id' > $ group_id,
'Staff.isActive ='=>1,
'Staff.last_name LIKE'=>%$ name%,
)$ b b)
);

debug($ tmp);






  / $ tmpConditions ['AND'] [] = array('StaffStaffgroup.staffgroup_id'=> $ group_ids); 






EDIT: / p>

我尝试使用条件和可控制的行为,但不能过滤任何东西

 code> $ this-> Staff-> contains(array('StaffStaffgroup')); 
$ this-> paginate = array('StaffStaffgroup'=> array(
array('conditions'=> array(
'StaffStaffgroup.staffgroup_id'=&



);




  • 我添加到所有模型:public $ actsAs = array ');

  • 我也尝试了一个内部连接,但没有过滤:

      $ this-> paginate = array(
    'conditions'=> array('StaffStaffgroup.staffgroup_id'=> 2),
    'joins'=& b array(
    'alias'=>'StaffStaffgroup',
    'table'=>'staff_staffgroups',
    'type'=>'INNER',
    ' conditions'=>'StaffGroup_id = StaffStaffgroup.staffgroup_id'


    );



解决方案

您不能对此使用Containable行为。检查sql dump,你会看到查询完成。



我会在两个步骤中完成



From StaffStaffgroup获取属于您想要的组的职员ID

  $ staff_ids = $ this-> Staff-> StaffStaffgroup-> find(
'all',
array(
'conditions'=> array('StaffStaffgroup.staffgroup_id'=> $ group_id,),
' recursive'=> -1,
'fields'=> array('StaffStaffgroup.staff_id'),

);

然后让所有工作人员使用上一个结果

  $ staffs = $ this-> Staff-> find(
'all',
array(
'conditions'=> array (
'Staff.id'=> $ staff_ids,
'Staff.isActive ='=>1,
'Staff.last_name LIKE'=>%$名%,
),

);


preamble: a few days ago I asked a question to solve a HABTM-filter, I'm not able to do it even with tutorials, so "Obi Kwan Kenobi youre my only hope".

What I want to achieve: Filtering Staff by GroupID which is used in StaffStaffgroup

I'm having the following tablelayout

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

The variable $tmp gets me a working array, but the problem is that Staff is a child object of StaffStaffgroups. I could parse throu and reassemble a array, but this isnt a nice solution. So I want to use the condition on Staff (see comented line) but then I get the error 1054 "column not found: 1054 Unknown column". I tried to bind and unbind, but no result there.

$group_id = 2;
$tmp = $this->Staff->StaffStaffgroup->find('all',
        array('conditions' => array(
            'StaffStaffgroup.staffgroup_id' => $group_id,
            'Staff.isActive =' => "1",
            'Staff.last_name LIKE' => "%$name%",
            )
         )
);

debug($tmp);


//$tmpConditions['AND'][] = array('StaffStaffgroup.staffgroup_id' => $group_ids);


EDIT:

I tried it with conditions and containable behaviour, but unfortunatelly its not filtering anything at all

    $this->Staff->contain(array('StaffStaffgroup'));
    $this->paginate = array('StaffStaffgroup' =>array(
                                    array('conditions'  => array(
                                            'StaffStaffgroup.staffgroup_id' => '2'
                                        )
                                    )
                                )
    );

  • I added to all models: public $actsAs = array('Containable');
  • I tried also with an inner join but no filtering there:

     $this->paginate = array( 
     'conditions' => array('StaffStaffgroup.staffgroup_id' => 2 ),
     'joins' => array(
        array(
            'alias' => 'StaffStaffgroup',
            'table' => 'staff_staffgroups',
            'type' => 'INNER',
            'conditions' => 'StaffGroup_id = StaffStaffgroup.staffgroup_id'
        )
     )
    

    );

解决方案

You cannot use the Containable behavior for this. Checkout the sql dump, you will see that the query is done.

I would do it in two steps

From StaffStaffgroup get the staff ids that belongs to the group you want

$staff_ids = $this->Staff->StaffStaffgroup->find(
    'all', 
    array(
        'conditions' => array('StaffStaffgroup.staffgroup_id' => $group_id, ),
        'recursive' => -1,
        'fields' => array('StaffStaffgroup.staff_id'),
    )
);

Then get all staff using the previous result

$staffs = $this->Staff->find(
    'all', 
    array(
        'conditions' => array(
            'Staff.id' => $staff_ids, 
            'Staff.isActive =' => "1",
            'Staff.last_name LIKE' => "%$name%",
        ),
    )
);

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

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