Remove methods replacing query condition
https://github.com/yiisoft/db/blob/master/src/Query/QueryPartsInterface.php#L642
Methods like where() are often the source of problems cause sometimes these are used instead of andWhere(), orWhere().
Possible solutions:
- Remove such methods. Add reset*() for the rare cases it is needed.
- Make where() the same as andWhere(). Add reset*() for the rare cases it is needed.
Use case for the current implementation of where()
$query = (new Query($db))->from('table_name');
foreach ($list as $item) {
$count = $query->where(['field' => $item])->count();
...
}
For the issues, renaming it to withWhere() would be enough
$count = $query->withWhere(['field' => $item])->count();
For me the current implementation of where() looks better. Perhaps this is a documentation issue for cases when need to use andWhere() or orWhere() instead of where().
1. Remove such methods. Add reset*() for the rare cases it is needed.
It is will be counter intuitive:
$query->whe // ???
// Why IDE don't suggest method where?
// Why not exist method where???
// Oh, need `andWhere`
Also it's poorly readable:
$query->andWhere(...)->all();
Also this suggestion will lead to inconsistency with methods orderBy(), select(), and so on. Change these methods will leed to more poorly readable.
2. Make where() the same as andWhere(). Add reset*() for the rare cases it is needed.
This suggesion is better, but in this case need change behavior of methods orderBy(), select(), and so on also.
Also reset*() methods improve code clarity:
$query = (new Query($db))->from('table_name');
// current
foreach ($list as $item) {
$count = $query->where(['field' => $item])->count();
...
}
// suggestion
foreach ($list as $item) {
$count = $query->resetWhere()->where(['field' => $item])->count();
...
}
I like this way. despite it is break of BC.
If cases when methods where(), select(), etc. used for override previous values is rare, we can definitely do it.
@Tigrov Renaming it to withWhere() don't resolve issue, because problem in usage exactly where() method which is used several times, thinking that conditionals are accumulating.
The second suggestion looks good.
foreach ($list as $item) {
$count = $query->resetWhere()->where(['field' => $item])->count();
...
}