Skip to content

Commit

Permalink
Merge pull request #6765 from cakephp/issue-6739
Browse files Browse the repository at this point in the history
Correct the parameter type used in function expressions.
  • Loading branch information
lorenzo committed Jun 9, 2015
2 parents 9df927f + 6639b0d commit 7a783cb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/Database/Expression/FunctionExpression.php
Expand Up @@ -95,6 +95,7 @@ public function name($name = null)
public function add($params, $types = [], $prepend = false)
{
$put = $prepend ? 'array_unshift' : 'array_push';
$typeMap = $this->typeMap()->types($types);
foreach ($params as $k => $p) {
if ($p === 'literal') {
$put($this->_conditions, $k);
Expand All @@ -105,9 +106,7 @@ public function add($params, $types = [], $prepend = false)
$put($this->_conditions, $p);
continue;
}

$type = isset($types[$k]) ? $types[$k] : null;
$put($this->_conditions, ['value' => $p, 'type' => $type]);
$put($this->_conditions, ['value' => $p, 'type' => $typeMap->type($k)]);
}

return $this;
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -878,4 +878,30 @@ public function testFindLastOnEmptyTable()
$this->assertEquals(0, $table->find()->count());
$this->assertNull($table->find()->last());
}

/**
* Test that the typemaps used in function expressions
* create the correct results.
*
* @return void
*/
public function testTypemapInFunctions()
{
$table = TableRegistry::get('Comments');
$table->updateAll(['published' => null], ['1 = 1']);
$query = $table->find();
$query->select([
'id',
'coalesced' => $query->func()->coalesce(
['published' => 'literal', -1],
['integer']
)
]);
$result = $query->all()->first();
$this->assertSame(
'-1',
$result['coalesced'],
'Output values for functions are not cast yet.'
);
}
}
3 changes: 1 addition & 2 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -835,8 +835,7 @@ public function testApplyOptions()

$this->assertEquals(1, $query->clause('limit'));

$expected = new QueryExpression(['a > b']);
$expected->typeMap($this->fooTypeMap);
$expected = new QueryExpression(['a > b'], $this->fooTypeMap);
$result = $query->clause('join');
$this->assertEquals([
'table_a' => ['alias' => 'table_a', 'type' => 'INNER', 'conditions' => $expected]
Expand Down

0 comments on commit 7a783cb

Please sign in to comment.