Skip to content

Commit

Permalink
Only update autoFields() if it is undefined.
Browse files Browse the repository at this point in the history
If a query builder has enabled/disabled autoFields() we should preserve
the developer's choice.

Refs #8052
  • Loading branch information
markstory committed Jan 25, 2016
1 parent 1812bc9 commit 9de28a7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ORM/Association/BelongsToMany.php
Expand Up @@ -1204,13 +1204,18 @@ protected function _buildQuery($options)
}

$query = $this->_appendJunctionJoin($query, []);

if ($query->autoFields() === null) {
$query->autoFields($query->clause('select') === []);
}

// Ensure that association conditions are applied
// and that the required keys are in the selected columns.
$query
->where($this->junctionConditions())
->autoFields($query->clause('select') === [])
->select($query->aliasFields((array)$assoc->foreignKey(), $name));


$assoc->attachTo($query);
return $query;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -980,6 +980,27 @@ public function testEagerLoadingBelongsToManyLimitedFields()
$this->assertEmpty($result->tags[0]->name);
}

/**
* Tests that fetching belongsToMany association will retain autoFields(true) if it was used.
*
* @see https://github.com/cakephp/cakephp/issues/8052
* @return void
*/
public function testEagerLoadingBelongsToManyLimitedFieldsWithAutoFields()
{
$table = TableRegistry::get('Articles');
$table->belongsToMany('Tags');
$result = $table
->find()
->contain(['Tags' => function ($q) {
return $q->select(['two' => '1 + 1'])->autoFields(true);
}])
->first();

$this->assertNotEmpty($result->tags[0]->two, 'Should have computed field');
$this->assertNotEmpty($result->tags[0]->name, 'Should have standard field');
}

/**
* Test that association proxy find() applies joins when conditions are involved.
*
Expand Down

0 comments on commit 9de28a7

Please sign in to comment.