Skip to content

Commit

Permalink
Correct the parameter type used in function expressions.
Browse files Browse the repository at this point in the history
Only using the non-default types doesn't provide access to the typehints
that were provided during the expression creation. This causes bound
parameters to be incorrect. While this change corrects the type of the bound
parameter it does not fix the selected value's type.

Refs #6739
  • Loading branch information
markstory committed Jun 9, 2015
1 parent b8e00f5 commit 6639b0d
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 6639b0d

Please sign in to comment.