我正在使用CakePHP 3.2并在find()之后。
if($filter_data_array['age_younger'] != '')
{
$conditions["icases.age <= "] = $filter_data_array['age_younger'];
}
if($filter_data_array['age_older'] != '')
{
$conditions["icases.age >= "] = $filter_data_array['age_older'];
}
if($filter_data_array['go_date_after'] != '')
{
$conditions["icases.go_date >= "] = $filter_data_array['go_date_after'];
}
if($filter_data_array['go_date_before'] != '')
{
$conditions["icases.go_date <= "] = $filter_data_array['go_date_before'];
}
if($filter_data_array['client'] != '')
{
$conditions["icases.client_id = "] = $filter_data_array['client'];
}
if($filter_data_array['case_name'] != '')
{
$conditions["icases.name = "] = $filter_data_array['case_name'];
}
if($filter_data_array['case_ref'] != '')
{
$conditions["icases.case_reference_i_d = "] = $filter_data_array['case_ref'];
}
if($filter_data_array['instruction_date'] != '')
{
$conditions["icases.date_instruction_received >= "] = $filter_data_array['instruction_date'];
}
$cases_data = TableRegistry::get('icases')->find('all')
->select(['icases.id', 'icases.state', 'icases.client_id', 'icases.name', 'icases.age', 'icases.case_reference_i_d', 'icases.go_date', 'clients.name'])
->innerJoin('icases_users', 'icases_users.icase_id = icases.id')
->where($conditions)
->innerJoin('clients', 'clients.id = icases.client_id')
->order(['clients.name' => 'ASC', 'icases.name' => 'ASC'])
->execute()
->fetchAll('assoc');
现在icases表中有一个状态列,我希望按
进行过滤icases.state='active' OR icases.state='pending' OR icases.state='archive'
我的问题是如何将其集成到现有的$ conditions数组中?
提前谢谢。
答案 0 :(得分:1)
我认为你应该使用蛋糕提供的Advanced Conditions。根据我的理解,第一个例子适合您的需要。
所以你应该在执行查询之前添加它
$conditions['OR'] = [['icases.state' => 'active'], ['icases.state' => 'pending'], ['icases.state' => 'archive']];
答案 1 :(得分:1)
以下是找到与
OR
条件匹配的所有内容的解决方案
$conditions = []; #For storing all of your conditions as an array
if($filter_data_array['age_younger'] != ''){
$conditions[]["icases.age <= "] = $filter_data_array['age_younger'];
}
if($filter_data_array['age_older'] != ''){
$conditions[]["icases.age >= "] = $filter_data_array['age_older'];
}
if($filter_data_array['go_date_after'] != ''){
$conditions[]["icases.go_date >= "] = $filter_data_array['go_date_after'];
}
if($filter_data_array['go_date_before'] != ''){
$conditions[]["icases.go_date <= "] = $filter_data_array['go_date_before'];
}
if($filter_data_array['client'] != ''){
$conditions[]["icases.client_id = "] = $filter_data_array['client'];
}
if($filter_data_array['case_name'] != ''){
$conditions[]["icases.name = "] = $filter_data_array['case_name'];
}
if($filter_data_array['case_ref'] != ''){
$conditions[]["icases.case_reference_i_d = "] = $filter_data_array['case_ref'];
}
if($filter_data_array['instruction_date'] != ''){
$conditions[]["icases.date_instruction_received >= "] = $filter_data_array['instruction_date'];
}
$cases_data = TableRegistry::get('icases')->find('all')
->select(['icases.id', 'icases.state', 'icases.client_id', 'icases.name', 'icases.age', 'icases.case_reference_i_d', 'icases.go_date', 'clients.name'])
->innerJoin('icases_users', 'icases_users.icase_id = icases.id')
->where(['OR'=>$conditions]) #All of your `OR` conditions here
->innerJoin('clients', 'clients.id = icases.client_id')
->order(['clients.name' => 'ASC', 'icases.name' => 'ASC'])
->execute()
->fetchAll('assoc');
注意:您的代码已更改为 -
$conditions[INDEX] to $conditions[][INDEX]
和
->where($conditions) to ->where(['OR'=>$conditions])
答案 2 :(得分:0)
首先,我建议您关注CakePHP Conventions。您的代码将变得更具可读性。
我还建议在Table对象中使用connect your relations。这样可以保持代码干燥,您只需使用contain
而不是使用innerJoin
“手动”加载相关模型。
查看以下代码。它没有经过测试,但应该可行。使用Advanced conditions更像'Cake-ish'并且很容易保持可持续性。如果您不关心其余部分,请查看代码中的$orConditions
。
$query = TableRegistry::get('Cases')->find('all')
->select([
'Cases.id',
'Cases.state',
'Cases.client_id',
'Cases.name',
'Cases.age',
'Cases.case_reference_i_d',
'Cases.go_date',
'Clients.name'
])
->order([
'Clients.name' => 'asc',
'Cases.name' => 'asc'
])
->contain([
'Clients',
'Users'
])
->where(function ($exp) use ($filter_data_array) {
if (!empty($filter_data_array['age_younger'])) {
$exp = $exp->lte('Cases.age', $filter_data_array['age_younger']);
}
if (!empty($filter_data_array['age_older'])) {
$exp = $exp->gte('Cases.age', $filter_data_array['age_older']);
}
if (!empty($filter_data_array['go_date_after'])) {
$exp = $exp->gte('Cases.go_date', $filter_data_array['go_date_after']);
}
if (!empty($filter_data_array['go_date_before'])) {
$exp = $exp->lte('Cases.go_date', $filter_data_array['go_date_before']);
}
if (!empty($filter_data_array['client'])) {
$exp = $exp->eq('Cases.client_id', $filter_data_array['client']);
}
if (!empty($filter_data_array['case_name'])) {
$exp = $exp->eq('Cases.name', $filter_data_array['case_name']);
}
if (!empty($filter_data_array['case_ref'])) {
$exp = $exp->eq('Cases.case_reference_i_d', $filter_data_array['case_ref']);
}
if (!empty($filter_data_array['instruction_date'])) {
$exp = $exp->gte('Cases.date_instruction_received', $filter_data_array['instruction_date']);
}
$orConditions = $exp->or_([
'Cases.state' => 'active',
'Cases.state' => 'pending',
'Cases.state' => 'archive'
]);
return $exp->add($orConditions);
});
$cases_data = $query->execute();