选择存在或空的条件

时间:2015-03-25 13:24:51

标签: cakephp-3.0

在CakePHP 3项目中,我想在我的控制器中运行一个选择。用户可以为选择选择不同的过滤条件。如果没有选择条件,我在选择中不需要它。

现在我有类似的东西

$where = [];

if ( ! empty($this->request->data['fcontact_id'])){
    $where['Histories.contact_id'] = $this->request->data['fcontact_id'];
}
if ( ! empty($this->request->data['fuser_id'])){
    $where['Histories.user_id'] = $this->request->data['fuser_id'];
}
if ( ! empty($this->request->data['fgroup_id'])){
    $where['Histories.group_id'] = $this->request->data['fgroup_id'];
}

$histories = $this->Histories->find()->where($where);

有没有更好的方法呢?

1 个答案:

答案 0 :(得分:0)