Skip to content

Commit

Permalink
Fix BC break
Browse files Browse the repository at this point in the history
Ignore empty strings in QueryBuilder::and/orWhere() & and/orHaving().
  • Loading branch information
BenMorel committed Sep 22, 2020
1 parent 5d22823 commit 7401cda
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/Doctrine/DBAL/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
use Doctrine\DBAL\Query\Expression\CompositeExpression;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;

use function array_filter;
use function array_key_exists;
use function array_keys;
use function array_unshift;
use function array_values;
use function func_get_args;
use function func_num_args;
use function implode;
Expand Down Expand Up @@ -829,6 +831,7 @@ public function where($predicates)
public function andWhere($where)
{
$args = func_get_args();
$args = self::filterEmptyArgs($args);
$where = $this->getQueryPart('where');

if ($where instanceof CompositeExpression && $where->getType() === CompositeExpression::TYPE_AND) {
Expand Down Expand Up @@ -862,6 +865,7 @@ public function andWhere($where)
public function orWhere($where)
{
$args = func_get_args();
$args = self::filterEmptyArgs($args);
$where = $this->getQueryPart('where');

if ($where instanceof CompositeExpression && $where->getType() === CompositeExpression::TYPE_OR) {
Expand Down Expand Up @@ -1010,6 +1014,7 @@ public function having($having)
public function andHaving($having)
{
$args = func_get_args();
$args = self::filterEmptyArgs($args);
$having = $this->getQueryPart('having');

if ($having instanceof CompositeExpression && $having->getType() === CompositeExpression::TYPE_AND) {
Expand All @@ -1033,6 +1038,7 @@ public function andHaving($having)
public function orHaving($having)
{
$args = func_get_args();
$args = self::filterEmptyArgs($args);
$having = $this->getQueryPart('having');

if ($having instanceof CompositeExpression && $having->getType() === CompositeExpression::TYPE_OR) {
Expand Down Expand Up @@ -1393,4 +1399,21 @@ public function __clone()
$this->params[$name] = clone $param;
}
}

/**
* Filters out empty values from the given argument list.
* Fixes a BC break introduces in 2.11: https://github.com/doctrine/dbal/issues/4282
*
* @param array<mixed> $args
*
* @return array<mixed>
*
* @todo remove for 3.0
*/
private static function filterEmptyArgs(array $args): array
{
return array_values(array_filter($args, static function ($value) {
return ! empty($value);
}));
}
}
13 changes: 13 additions & 0 deletions tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -949,4 +949,17 @@ public function testJoinWithNonUniqueAliasThrowsException(): void

$qb->getSQL();
}

public function testAndWhereEmptyString(): void
{
$qb = new QueryBuilder($this->conn);

$qb->select('id')
->from('foo')
->where('a = b');

$qb->andWhere('', "foo = 'bar'");

self::assertSame("SELECT id FROM foo WHERE (a = b) AND (foo = 'bar')", $qb->getSQL());
}
}

0 comments on commit 7401cda

Please sign in to comment.