Skip to content

Commit

Permalink
Fix empty clauses causing errors in matching().
Browse files Browse the repository at this point in the history
Instead of null the where clause should be set to an empty expression.
Storing null causes issues in other parts of the framework.

Refs #7102
  • Loading branch information
markstory committed Jul 24, 2015
1 parent fa54940 commit ffb14bc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Database/Query.php
Expand Up @@ -1643,10 +1643,11 @@ protected function _decorateStatement($statement)
*/
protected function _conjugate($part, $append, $conjunction, $types)
{
$expression = $this->_parts[$part] ?: $this->newExpr();
if (empty($append)) {
$this->_parts[$part] = $expression;
return;
}
$expression = $this->_parts[$part] ?: $this->newExpr();

if (!is_string($append) && is_callable($append)) {
$append = $append($this->newExpr(), $this);
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -1190,10 +1190,10 @@ public function testWhereEmptyValues()
$query->from('comments')
->where('');

$this->assertNull($query->clause('where'));
$this->assertCount(0, $query->clause('where'));

$query->where([]);
$this->assertNull($query->clause('where'));
$this->assertCount(0, $query->clause('where'));
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -879,6 +879,31 @@ public function testMatchingWithNoFields()
$this->assertEquals([2], $results);
}

/**
* Test that empty conditions in a matching clause don't cause errors.
*
* @return void
*/
public function testMatchingEmptyQuery()
{
$table = TableRegistry::get('Articles');
$table->belongsToMany('Tags');

$rows = $table->find()
->matching('Tags', function ($q) {
return $q->where([]);
})
->all();
$this->assertNotEmpty($rows);

$rows = $table->find()
->matching('Tags', function ($q) {
return $q->where(null);
})
->all();
$this->assertNotEmpty($rows);
}

/**
* Tests that using a subquery as part of an expression will not make invalid SQL
*
Expand Down

0 comments on commit ffb14bc

Please sign in to comment.