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]