diff --git a/src/Database/Query.php b/src/Database/Query.php index f553056d508..95aa092a00a 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -755,6 +755,9 @@ protected function _makeJoin($table, $conditions, $type) */ public function where($conditions = null, $types = [], $overwrite = false) { + if (empty($conditions)) { + return $this; + } if ($overwrite) { $this->_parts['where'] = $this->newExpr(); } diff --git a/tests/TestCase/Database/QueryTest.php b/tests/TestCase/Database/QueryTest.php index 1ea3a4a1357..752ee22a42b 100644 --- a/tests/TestCase/Database/QueryTest.php +++ b/tests/TestCase/Database/QueryTest.php @@ -1179,6 +1179,23 @@ public function testInValueCast2() $this->assertCount(5, $result); } + /** + * Tests that empty values don't set where clauses. + * + * @return void + */ + public function testWhereEmptyValues() + { + $query = new Query($this->connection); + $query->from('comments') + ->where(''); + + $this->assertNull($query->clause('where')); + + $query->where([]); + $this->assertNull($query->clause('where')); + } + /** * Tests that it is possible to use a between expression * in a where condition diff --git a/tests/TestCase/ORM/AssociationProxyTest.php b/tests/TestCase/ORM/AssociationProxyTest.php index a6c6c82bfac..98846dfd552 100644 --- a/tests/TestCase/ORM/AssociationProxyTest.php +++ b/tests/TestCase/ORM/AssociationProxyTest.php @@ -77,6 +77,22 @@ public function testGetBadAssociation() $articles->posts; } + /** + * Test that find() with empty conditions generates valid SQL + * + * @return void + */ + public function testFindEmptyConditions() + { + $table = TableRegistry::get('Users'); + $table->hasMany('Articles', [ + 'foreignKey' => 'author_id', + 'conditions' => '', + ]); + $query = $table->Articles->find('list', ['limit' => 2]); + $this->assertCount(2, $query->all()); + } + /** * Tests that the proxied updateAll will preserve conditions set for the association *