Skip to content

Commit

Permalink
Adding all association fields to the select if none is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 4, 2013
1 parent 915ae6d commit 5d54978
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/Cake/ORM/Query.php
Expand Up @@ -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);
}
Expand Down
60 changes: 56 additions & 4 deletions lib/Cake/Test/TestCase/ORM/QueryTest.php
Expand Up @@ -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]
]
]];

Expand Down Expand Up @@ -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',
Expand All @@ -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.