Browse Source

Merge pull request #15738 from cakephp/issue-13049

Fix type specified for FunctionExpression not being respected.
Mark Story 4 years ago
parent
commit
7dc36c2d03
2 changed files with 27 additions and 3 deletions
  1. 4 3
      src/ORM/Query.php
  2. 23 0
      tests/TestCase/ORM/QueryRegressionTest.php

+ 4 - 3
src/ORM/Query.php

@@ -1215,6 +1215,10 @@ class Query extends DatabaseQuery implements JsonSerializable, QueryInterface
         $types = [];
 
         foreach ($select as $alias => $value) {
+            if ($value instanceof TypedResultInterface) {
+                $types[$alias] = $value->getReturnType();
+                continue;
+            }
             if (isset($typeMap[$alias])) {
                 $types[$alias] = $typeMap[$alias];
                 continue;
@@ -1222,9 +1226,6 @@ class Query extends DatabaseQuery implements JsonSerializable, QueryInterface
             if (is_string($value) && isset($typeMap[$value])) {
                 $types[$alias] = $typeMap[$value];
             }
-            if ($value instanceof TypedResultInterface) {
-                $types[$alias] = $value->getReturnType();
-            }
         }
         $this->getSelectTypeMap()->addDefaults($types);
     }

+ 23 - 0
tests/TestCase/ORM/QueryRegressionTest.php

@@ -1199,6 +1199,29 @@ class QueryRegressionTest extends TestCase
     }
 
     /**
+     * Test that the type specified in function expressions takes priority over
+     * default types set for columns.
+     *
+     * @see https://github.com/cakephp/cakephp/issues/13049
+     * @return void
+     */
+    public function testTypemapInFunctions3(): void
+    {
+        $this->loadFixtures('Comments');
+        $table = $this->getTableLocator()->get('Comments');
+        $query = $table->find();
+
+        $result = $query->select(['id' => $query->func()->min('id')])
+            ->first();
+        $this->assertSame(1.0, $result['id']);
+
+        $query = $table->find();
+        $result = $query->select(['id' => $query->func()->min('id', ['boolean'])])
+            ->first();
+        $this->assertTrue($result['id']);
+    }
+
+    /**
      * Test that contain queries map types correctly.
      */
     public function testBooleanConditionsInContain(): void