end*_*nda 5 filtering cakephp model conditional-statements
序言:前几天我问了一个问题来解决一个HABTM过滤器,我甚至不能用教程来做,所以"Obi Kwan Kenobi你是我唯一的希望".
我想要实现的目标:按StaffStaffgroup中使用的GroupID过滤员工
我有以下tablelayout
变量$ tmp给我一个工作数组,但问题是Staff是StaffStaffgroups的子对象.我可以解析throu并重新组装一个数组,但这不是一个很好的解决方案.所以我想在Staff上使用条件(参见注释行),但后来我得到错误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%",
)
)
);
debug($tmp);
Run Code Online (Sandbox Code Playgroud)
//$tmpConditions['AND'][] = array('StaffStaffgroup.staffgroup_id' => $group_ids);
Run Code Online (Sandbox Code Playgroud)
编辑:
我尝试了条件和可容纳的行为,但不幸的是它根本没有过滤任何东西
$this->Staff->contain(array('StaffStaffgroup'));
$this->paginate = array('StaffStaffgroup' =>array(
array('conditions' => array(
'StaffStaffgroup.staffgroup_id' => '2'
)
)
)
);
Run Code Online (Sandbox Code Playgroud)
我也尝试了内连接,但没有过滤:
$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'
)
)
Run Code Online (Sandbox Code Playgroud)
);
您不能使用Containable行为.签出sql转储,您将看到查询已完成.
我会分两步完成
从StaffStaffgroup获取属于您想要的组的员工ID
$staff_ids = $this->Staff->StaffStaffgroup->find(
'all',
array(
'conditions' => array('StaffStaffgroup.staffgroup_id' => $group_id, ),
'recursive' => -1,
'fields' => array('StaffStaffgroup.staff_id'),
)
);
Run Code Online (Sandbox Code Playgroud)
然后让所有员工使用之前的结果
$staffs = $this->Staff->find(
'all',
array(
'conditions' => array(
'Staff.id' => $staff_ids,
'Staff.isActive =' => "1",
'Staff.last_name LIKE' => "%$name%",
),
)
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1713 次 |
| 最近记录: |