From 26fcec6ec827fad6688f0a70c5a63d236ebe4e92 Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Thu, 8 Oct 2020 19:19:35 +0200 Subject: [PATCH] Fix issue #4282, again --- lib/Doctrine/DBAL/Query/QueryBuilder.php | 9 +++++-- .../Tests/DBAL/Query/QueryBuilderTest.php | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/DBAL/Query/QueryBuilder.php b/lib/Doctrine/DBAL/Query/QueryBuilder.php index bb8dce8187e..1d66b9e63c6 100644 --- a/lib/Doctrine/DBAL/Query/QueryBuilder.php +++ b/lib/Doctrine/DBAL/Query/QueryBuilder.php @@ -12,6 +12,7 @@ use function array_key_exists; use function array_keys; use function array_unshift; +use function count; use function func_get_args; use function func_num_args; use function implode; @@ -834,7 +835,9 @@ public function andWhere($where) $where = $this->getQueryPart('where'); if ($where instanceof CompositeExpression && $where->getType() === CompositeExpression::TYPE_AND) { - $where = $where->with(...$args); + if (count($args) > 0) { + $where = $where->with(...$args); + } } else { array_unshift($args, $where); $where = CompositeExpression::and(...$args); @@ -868,7 +871,9 @@ public function orWhere($where) $where = $this->getQueryPart('where'); if ($where instanceof CompositeExpression && $where->getType() === CompositeExpression::TYPE_OR) { - $where = $where->with(...$args); + if (count($args) > 0) { + $where = $where->with(...$args); + } } else { array_unshift($args, $where); $where = CompositeExpression::or(...$args); diff --git a/tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php b/tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php index 038097c8910..a83362ed588 100644 --- a/tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php +++ b/tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php @@ -950,6 +950,19 @@ public function testJoinWithNonUniqueAliasThrowsException(): void $qb->getSQL(); } + public function testWhereExpressionAndWhereEmptyString(): void + { + $qb = new QueryBuilder($this->conn); + + $qb->select('id') + ->from('foo') + ->where('a = b'); + + $qb->andWhere(''); + + self::assertSame('SELECT id FROM foo WHERE a = b', $qb->getSQL()); + } + public function testAndWhereEmptyStringStartingWithEmptyExpression(): void { $qb = new QueryBuilder($this->conn); @@ -975,6 +988,19 @@ public function testAndWhereEmptyStringStartingWithNonEmptyExpression(): void self::assertSame('SELECT id FROM foo WHERE (a = b) AND (c = d)', $qb->getSQL()); } + public function testWhereExpressionOrWhereEmptyString(): void + { + $qb = new QueryBuilder($this->conn); + + $qb->select('id') + ->from('foo') + ->orWhere('a = b'); + + $qb->orWhere(''); + + self::assertSame('SELECT id FROM foo WHERE a = b', $qb->getSQL()); + } + public function testOrWhereEmptyStringStartingWithEmptyExpression(): void { $qb = new QueryBuilder($this->conn);