Skip to content
Permalink
Browse files

Adding all association fields to the select if none is specified

  • Loading branch information...
lorenzo committed May 4, 2013
1 parent 915ae6d commit 5d54978527c6fc921bef8e02c0f5533f69b3b314
Showing with 59 additions and 4 deletions.
  1. +3 −0 lib/Cake/ORM/Query.php
  2. +56 −4 lib/Cake/Test/TestCase/ORM/QueryTest.php
@@ -119,6 +119,9 @@ protected function _normalizeContain(Table $parent, $alias, $options) {
];
$config = $this->_resolveForeignKeyConditions($table, $parent, $config);
if (empty($config['fields']) || $config['fields'] !== false) {
$config['fields'] = array_keys($table->schema());
}
foreach ($extra as $t => $assoc) {
$config['associations'][$t] = $this->_normalizeContain($table, $t, $assoc);
}
@@ -44,19 +44,24 @@ public function testContainToJoinsOneLevel() {
$contains = ['client' => [
'associationType' => 'belongsTo',
'table' => 'clients',
'fields' => false,
'order' => [
'associationType' => 'hasOne',
'orderType' => ['associationType' => 'belongsTo'],
'orderType' => ['associationType' => 'belongsTo', 'fields' => false],
'fields' => false,
'stuff' => [
'associationType' => 'hasOne', 'table' => 'things',
'stuffType' => ['associationType' => 'belongsTo']
'associationType' => 'hasOne',
'table' => 'things',
'fields' => false,
'stuffType' => ['associationType' => 'belongsTo', 'fields' => false]
]
],
'company' => [
'associationType' => 'belongsTo',
'fields' => false,
'table' => 'organizations',
'foreignKey' => 'organization_id',
'category' => ['associationType' => 'belongsTo']
'category' => ['associationType' => 'belongsTo', 'fields' => false]
]
]];
@@ -126,6 +131,11 @@ public function testContainToJoinsOneLevel() {
->contain($contains)->sql();
}
/**
* Test that fields for contained models are aliased and added to the select clause
*
* @return void
**/
public function testContainToFieldsPredefined() {
$contains = ['client' => [
'associationType' => 'belongsTo',
@@ -151,4 +161,46 @@ public function testContainToFieldsPredefined() {
$this->assertEquals($expected, $select);
}
/**
* Tests that default fields for associations are added to the select clause when
* none is specified
*
* @return void
**/
public function testContainToFieldsDefault() {
$contains = [
'client' => [
'associationType' => 'belongsTo',
'order' => [
'associationType' => 'hasOne',
]
]
];
$schema1 = [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
'phone' => ['type' => 'string']
];
$schema2 = [
'id' => ['type' => 'integer'],
'total' => ['type' => 'string'],
'placed' => ['type' => 'datetime']
];
$table = Table::build('foo', ['schema' => ['id' => ['type' => 'integer']]]);
Table::build('client', ['schema' => $schema1]);
Table::build('order', ['schema' => $schema2]);
$query = new Query($this->connection);
$query->select()->repository($table)->contain($contains)->sql();
$select = $query->clause('select');
$expected = [
'foo__id' => 'foo.id', 'client__name' => 'client.name',
'client__id' => 'client.id', 'client__phone' => 'client.phone',
'order__id' => 'order.id', 'order__total' => 'order.total',
'order__placed' => 'order.placed'
];
$this->assertEquals($expected, $select);
}
}

0 comments on commit 5d54978

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