Skip to content
Permalink
Browse files

Add result type casting for simple column aliases.

When columns in the schema are aliased to other fields we can easily and
correctly infer the result set types. Doing this makes the result sets
consistent when columns are aliased and when they are not.

Refs #9402
  • Loading branch information...
markstory committed Sep 5, 2016
1 parent 7bc0766 commit cf8ba17dccd7c3b5d4e07ced44da7c17bd824e81
Showing with 19 additions and 0 deletions.
  1. +3 −0 src/ORM/Query.php
  2. +16 −0 tests/TestCase/ORM/QueryTest.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();
}
@@ -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
*

0 comments on commit cf8ba17

Please sign in to comment.
You can’t perform that action at this time.