Skip to content

Commit

Permalink
Don't modify the where clause on empty inputs.
Browse files Browse the repository at this point in the history
When we get '' or [] we can safely assume that no where clause should be
set, as it would result in an invalid query.

Refs #7028
  • Loading branch information
markstory committed Jul 17, 2015
1 parent 5ea4003 commit 5597ec9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Database/Query.php
Expand Up @@ -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();
}
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/TestCase/ORM/AssociationProxyTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit 5597ec9

Please sign in to comment.