问题
I have many tables (user facing, not SQL) with filter boxes per column. The columns have not only the tables' fields but fields of related models.
I'm trying to add a feature where one can use a comma as an OR separator when filtering data. I need to do it generally as I have a rather large amount of tables, fields and relations. Plus some tables are built dynamically.
The code that adds the where clauses for the models own fields is this:
foreach ($filters as $column => $filter) {
$q->where(function ($q) use ($filter, $model, $column) {
$first = array_pop($filter);
$q->where("$model.$column", 'LIKE', "%$first%");
foreach ($filter as $or) {
$q->orWhere("$model.$column", 'LIKE', "%$or%");
}
});
}
This works as expected. When trying to do the same thing with a relations fields the only difference is using whereHas and including the model:
foreach ($filters as $column => $filter) {
$q->whereHas($model, function ($q) use ($filter, $model, $column) {
$first = array_pop($filter);
$q->where("$model.$column", 'LIKE', "%$first%");
foreach ($filter as $or) {
$q->orWhere("$model.$column", 'LIKE', "%$or%");
}
});
}
The problem is that if I filter on a relations fields it doesn't work as expected. I suspect it has to do with whereHas with a sub where/orWhere clause. This is the output I get
No filters:
|------------+-----------------------------------+--------------------------------------|
| Contact ID | Type (Field in contact SQL table) | Ecozone (Field in Ecozone SQL table) |
|------------+-----------------------------------+--------------------------------------|
| Filter -> | | |
|------------+-----------------------------------+--------------------------------------|
| 1 | Manager | Bush |
| 2 | | Forest |
| 3 | Worker | |
|------------+-----------------------------------+--------------------------------------|
Single filter on current model (correct):
|------------+-----------------------------------+--------------------------------------|
| Contact ID | Type (Field in contact SQL table) | Ecozone (Field in Ecozone SQL table) |
|------------+-----------------------------------+--------------------------------------|
| Filter -> | man | |
|------------+-----------------------------------+--------------------------------------|
| 1 | Manager | Bush |
|------------+-----------------------------------+--------------------------------------|
Multiple filter on current model (correct):
|------------+-----------------------------------+--------------------------------------|
| Contact ID | Type (Field in contact SQL table) | Ecozone (Field in Ecozone SQL table) |
|------------+-----------------------------------+--------------------------------------|
| Filter -> | man, wor | |
|------------+-----------------------------------+--------------------------------------|
| 1 | Manager | Bush |
| 3 | Worker | |
|------------+-----------------------------------+--------------------------------------|
Single filter on related model field (correct):
|------------+-----------------------------------+--------------------------------------|
| Contact ID | Type (Field in contact SQL table) | Ecozone (Field in Ecozone SQL table) |
|------------+-----------------------------------+--------------------------------------|
| Filter -> | | bus |
|------------+-----------------------------------+--------------------------------------|
| 1 | Manager | Bush |
|------------+-----------------------------------+--------------------------------------|
Multiple filter on related model field (incorrect):
|------------+-----------------------------------+--------------------------------------|
| Contact ID | Type (Field in contact SQL table) | Ecozone (Field in Ecozone SQL table) |
|------------+-----------------------------------+--------------------------------------|
| Filter -> | | bus,for |
|------------+-----------------------------------+--------------------------------------|
| 1 | Manager | Bush |
| 2 | | Forest |
| 3 | Worker | |
|------------+-----------------------------------+--------------------------------------|
The whole code path is below:
In controller:
return Contacts::with(
'ecozone'
)->where( function ($q) {
$this->set_filters($q, 'contact');
})->paginate($count);
In BaseController:
protected function set_filters($q, $current_model) {
$filters_array = $this->parse_filters();
if ($filters_array) {
foreach($filters_array as $model => $filters) {
if ($model == $current_model) {
foreach($filters as $column => $filter) {
$q->where(function ($q) use ($filter, $model, $column) {
$first = array_pop($filter);
$q->where("$model.$column", 'LIKE', "%$first%");
foreach($filter as $or) {
$q->orWhere("$model.$column", 'LIKE', "%$or%");
}
});
}
} else {
foreach($filters as $column => $filter) {
$q->whereHas($model, function ($q) use ($filter, $model, $column) {
$first = array_pop($filter);
$q->where("$model.$column", 'LIKE', "%$first%");
foreach($filter as $or) {
$q->orWhere("$model.$column", 'LIKE', "%$or%");
}
});
}
}
}
}
}
$this->filters is:
protected function parse_filters() {
$filters = Input::get('filters');
$filters = json_decode($filters);
$filters = array_where($filters, function($key, $value) {
return !empty($value);
});
$filters = (Array) $filters;
$has_filters = !empty($filters);
if ($has_filters) {
$filters_array = [];
foreach ($filters as $key => $value) {
$value = explode(',', $value);
array_set($filters_array, $key, $value);
}
} else {
$filters_array = false;
}
return $filters_array;
}
And returns, what I think is correct, the following array. First level is model, second is field, third is comma separated OR clauses
Array (
[ecozone] => Array
(
[ecozone] => Array
(
[0] => bush
[1] => forest
)
)
[contact] => Array
(
[contact_type] => Array
(
[0] => manager
[1] => worker
)
)
)
A dump of the SQL being used when filtering on current model fields. This works as expected:
array (size=4)
0 =>
array (size=3)
'query' => string 'select count(*) as aggregate from `contact` where `contact`.`deleted_at` is null and ((`contact`.`physical_address` LIKE ? or `contact`.`physical_address` LIKE ?))' (length=163)
'bindings' =>
array (size=2)
0 => string '%add%' (length=5)
1 => string '%nana%' (length=6)
'time' => float 1.67
1 =>
array (size=3)
'query' => string 'select * from `contact` where `contact`.`deleted_at` is null and ((`contact`.`physical_address` LIKE ? or `contact`.`physical_address` LIKE ?)) limit 10 offset 0' (length=161)
'bindings' =>
array (size=2)
0 => string '%add%' (length=5)
1 => string '%nana%' (length=6)
'time' => float 0.91
2 =>
array (size=3)
'query' => string 'select * from `ecozone` where `ecozone`.`deleted_at` is null and `ecozone`.`id` in (?, ?)' (length=89)
'bindings' =>
array (size=2)
0 => string '1' (length=1)
1 => string '2' (length=1)
'time' => float 0.8
A SQL query dump of filtering by a related models fields. This doesn't work as expected:
array (size=4)
0 =>
array (size=3)
'query' => string 'select count(*) as aggregate from `contact` where `contact`.`deleted_at` is null and ((select count(*) from `ecozone` where `ecozone`.`deleted_at` is null and `contact`.`ecozone_id` = `ecozone`.`id` and `ecozone`.`ecozone` LIKE ? or `ecozone`.`ecozone` LIKE ? and `ecozone`.`deleted_at` is null) >= 1)' (length=301)
'bindings' =>
array (size=2)
0 => string '%grass%' (length=7)
1 => string '%bush%' (length=6)
'time' => float 1.18
1 =>
array (size=3)
'query' => string 'select * from `contact` where `contact`.`deleted_at` is null and ((select count(*) from `ecozone` where `ecozone`.`deleted_at` is null and `contact`.`ecozone_id` = `ecozone`.`id` and `ecozone`.`ecozone` LIKE ? or `ecozone`.`ecozone` LIKE ? and `ecozone`.`deleted_at` is null) >= 1) limit 10 offset 0' (length=299)
'bindings' =>
array (size=2)
0 => string '%grass%' (length=7)
1 => string '%bush%' (length=6)
'time' => float 0.89
2 =>
array (size=3)
'query' => string 'select * from `ecozone` where `ecozone`.`deleted_at` is null and `ecozone`.`id` in (?, ?)' (length=89)
'bindings' =>
array (size=2)
0 => string '1' (length=1)
1 => string '2' (length=1)
'time' => float 1.38
回答1:
You need to use something like this for all your sets of filters:
`where(function($q){$q->where()->orWhere();})`
to make those inner wheres
wrapped in (...)
because by adding simple orWhere
you mess up whole query:
// your 2nd query in the incorrect array:
select * from `contact` where `contact`.`deleted_at` is null and
((select count(*) from `ecozone` where `ecozone`.`deleted_at` is null
and `contact`.`ecozone_id` = `ecozone`.`id`
// this is your first where()
and `ecozone`.`ecozone` LIKE ?
// and this is orWhere, which make the query wrong
or `ecozone`.`ecozone` LIKE ? and `ecozone`.`deleted_at` is null)
>= 1) limit 10 offset 0
You already did that for top level filters, but not in whereHas
So it should look like this:
} else {
foreach($filters as $column => $filter) {
$q->whereHas($model, function ($q) use ($filter, $model, $column) {
$q->where(function ($q) use ($filter, $model, $column) {
$first = array_pop($filter);
$q->where("$model.$column", 'LIKE', "%$first%");
foreach($filter as $or) {
$q->orWhere("$model.$column", 'LIKE', "%$or%");
}
});
});
}
}
来源:https://stackoverflow.com/questions/24687811/multiple-where-queries-using-orwhere-in-fields-of-model-and-related-models