Skip to content

Commit

Permalink
Add result type casting for simple column aliases.
Browse files Browse the repository at this point in the history
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 6, 2016
1 parent 7bc0766 commit cf8ba17
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/ORM/Query.php
Expand Up @@ -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();
}
Expand Down
16 changes: 16 additions & 0 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit cf8ba17

Please sign in to comment.