diff --git a/src/ORM/Query.php b/src/ORM/Query.php index 17f0f9db4dc..be052ebc586 100644 --- a/src/ORM/Query.php +++ b/src/ORM/Query.php @@ -1020,6 +1020,9 @@ protected function _addDefaultSelectTypes() $types[$alias] = $typeMap[$alias]; continue; } + if (is_string($value) && isset($typeMap[$value])) { + $types[$alias] = $typeMap[$value]; + } if ($value instanceof TypedResultInterface) { $types[$alias] = $value->returnType(); } diff --git a/tests/TestCase/ORM/QueryTest.php b/tests/TestCase/ORM/QueryTest.php index 0fe5e990712..d8760a4fbd3 100644 --- a/tests/TestCase/ORM/QueryTest.php +++ b/tests/TestCase/ORM/QueryTest.php @@ -2947,6 +2947,22 @@ public function testSelectWithTableAndAssociationInstance() $this->assertEquals($expected, $result); } + /** + * Test that simple aliased field have results typecast. + * + * @return void + */ + public function testSelectTypeInferSimpleAliases() + { + $table = TableRegistry::get('comments'); + $result = $table + ->find() + ->select(['created', 'updated_time' => 'updated']) + ->first(); + $this->assertInstanceOf(Time::class, $result->created); + $this->assertInstanceOf(Time::class, $result->updated_time); + } + /** * Tests that isEmpty() can be called on a query *