From 6639b0d47a2871a8432e74a423ed92a66a4b035a Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 7 Jun 2015 23:19:09 -0400 Subject: [PATCH] Correct the parameter type used in function expressions. 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 --- .../Expression/FunctionExpression.php | 5 ++-- tests/TestCase/ORM/QueryRegressionTest.php | 26 +++++++++++++++++++ tests/TestCase/ORM/QueryTest.php | 3 +-- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/Database/Expression/FunctionExpression.php b/src/Database/Expression/FunctionExpression.php index e1e7912cbf4..93aa0b4d8e2 100644 --- a/src/Database/Expression/FunctionExpression.php +++ b/src/Database/Expression/FunctionExpression.php @@ -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); @@ -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; diff --git a/tests/TestCase/ORM/QueryRegressionTest.php b/tests/TestCase/ORM/QueryRegressionTest.php index 60b627cb37c..6d79d092a1d 100644 --- a/tests/TestCase/ORM/QueryRegressionTest.php +++ b/tests/TestCase/ORM/QueryRegressionTest.php @@ -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.' + ); + } } diff --git a/tests/TestCase/ORM/QueryTest.php b/tests/TestCase/ORM/QueryTest.php index 7c89b2fb6b8..d243ae0cb5e 100644 --- a/tests/TestCase/ORM/QueryTest.php +++ b/tests/TestCase/ORM/QueryTest.php @@ -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]