Skip to content

Commit 22da3a5

Browse files
authored
Added ->setWhere() as force method ->where() (#944)
1 parent ae4858b commit 22da3a5

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
- New #939: Add `caseSensitive` option to like condition (@vjik)
7575
- New #942: Allow PHP backed enums as values (@Tigrov)
7676
- Enh #943: Add `getCacheKey()` and `getCacheTag()` methods to `AbstractPdoSchema` class (@Tigrov)
77+
- Enh #944: Added `setWhere()` as method a forced for overwriting `where()` (@lav45)
7778
- Enh #925, #951: Add callback to `Query::all()` and `Query::one()` methods (@Tigrov, @vjik)
7879
- New #954: Add `DbArrayHelper::arrange()` method (@Tigrov)
7980
- Chg #956: Remove nullable from `PdoConnectionInterface::getActivePdo()` result (@vjik)

UPGRADE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace
120120
- `QueryInterface::resultCallback()` - allows to use a callback, to be called on all rows of the query result;
121121
- `QueryInterface::getResultCallback()` - returns the callback to be called on all rows of the query result or
122122
`null` if the callback is not set;
123+
- `QueryPartsInterface::setWhere()` - overwrites the `WHERE` part of the query;
123124
- `ConnectionInterface::getColumnFactory()` - returns the column factory object for concrete DBMS;
124125
- `ConnectionInterface::getServerInfo()` - returns `ServerInfoInterface` instance which provides server information;
125126
- `QueryBuilderInterface::buildColumnDefinition()` - builds column definition for `CREATE TABLE` statement;

src/Query/Query.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yiisoft\Db\Query;
66

77
use Closure;
8+
use LogicException;
89
use Throwable;
910
use Yiisoft\Db\Command\CommandInterface;
1011
use Yiisoft\Db\Connection\ConnectionInterface;
@@ -694,6 +695,17 @@ public function union(QueryInterface|string $sql, bool $all = false): static
694695
}
695696

696697
public function where(array|string|ExpressionInterface|null $condition, array $params = []): static
698+
{
699+
if ($this->where === null) {
700+
$this->where = $condition;
701+
} else {
702+
throw new LogicException('The `where` condition was set earlier. Use the `setWhere()`, `andWhere()` or `orWhere()` method.');
703+
}
704+
$this->addParams($params);
705+
return $this;
706+
}
707+
708+
public function setWhere(array|string|ExpressionInterface|null $condition, array $params = []): static
697709
{
698710
$this->where = $condition;
699711
$this->addParams($params);

src/Query/QueryPartsInterface.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yiisoft\Db\Query;
66

77
use Closure;
8+
use LogicException;
89
use Yiisoft\Db\Connection\ConnectionInterface;
910
use Yiisoft\Db\Exception\NotSupportedException;
1011
use Yiisoft\Db\Expression\ExpressionInterface;
@@ -580,7 +581,7 @@ public function setUnions(array $value): static;
580581
public function union(QueryInterface|string $sql, bool $all = false): static;
581582

582583
/**
583-
* Sets the `WHERE` part of the query.
584+
* Initially sets the `WHERE` part of the query.
584585
*
585586
* The `$condition` specified as an array can be in one of the following two formats:
586587
*
@@ -671,11 +672,25 @@ public function union(QueryInterface|string $sql, bool $all = false): static;
671672
*
672673
* @psalm-param ParamsType $params
673674
*
675+
* @throws LogicException If `where` was set previously.
676+
*
674677
* @see andWhere()
675678
* @see orWhere()
676679
*/
677680
public function where(array|string|ExpressionInterface|null $condition, array $params = []): static;
678681

682+
/**
683+
* Overwrites the `WHERE` part of the query.
684+
*
685+
* @param array|ExpressionInterface|string|null $condition The conditions to put in the `WHERE` part.
686+
* @param array $params The parameters (name => value) to bind to the query.
687+
*
688+
* @psalm-param ParamsType $params
689+
*
690+
* @see where()
691+
*/
692+
public function setWhere(array|string|ExpressionInterface|null $condition, array $params = []): static;
693+
679694
/**
680695
* Prepends an SQL statement using `WITH` syntax.
681696
*

tests/AbstractQueryBuilderTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Closure;
88
use JsonException;
9+
use LogicException;
910
use PHPUnit\Framework\Attributes\DataProvider;
1011
use PHPUnit\Framework\Attributes\DataProviderExternal;
1112
use PHPUnit\Framework\TestCase;
@@ -306,7 +307,7 @@ public function testBuildIssue15653(): void
306307
307308
$qb = $db->getQueryBuilder();
308309
$query = (new Query($db))->from('admin_user')->where(['is_deleted' => false]);
309-
$query->where([])->andWhere(['in', 'id', ['1', '0']]);
310+
$query->setWhere([])->andWhere(['in', 'id', ['1', '0']]);
310311
311312
[$sql, $params] = $qb->build($query);
312313
@@ -474,6 +475,28 @@ public function testBuildLikeCondition(
474475
}
475476
}
476477
478+
/**
479+
* @throws LogicException
480+
*/
481+
public function testOverwriteWhereCondition(): void
482+
{
483+
$db = $this->getConnection();
484+
485+
try {
486+
(new Query($db))
487+
->where(['like', 'name', 'foo%'])
488+
->where(['not like', 'name', 'foo%']);
489+
} catch (LogicException $e) {
490+
$this->assertEquals('The `where` condition was set earlier. Use the `setWhere()`, `andWhere()` or `orWhere()` method.', $e->getMessage());
491+
}
492+
493+
$query = (new Query($db))
494+
->where(['like', 'name', 'foo%'])
495+
->setWhere(['not like', 'name', 'foo%']);
496+
497+
$this->assertEquals(['not like', 'name', 'foo%'], $query->getWhere());
498+
}
499+
477500
public function testBuildLimit(): void
478501
{
479502
$db = $this->getConnection();

0 commit comments

Comments
 (0)