Skip to content

Commit

Permalink
Don't set associated record to null when autofields is disabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Sep 29, 2016
1 parent 679d015 commit e084a14
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/ORM/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ class ResultSet implements ResultSetInterface
*/
protected $_hydrate = true;

/**
* Tracks value of $_autoFields property of $query passed to constructor.
*
* @var bool
*/
protected $_autoFields;

/**
* The fully namespaced name of the class to use for hydrating results
*
Expand Down Expand Up @@ -179,6 +186,7 @@ public function __construct($query, $statement)
$this->_defaultAlias = $this->_defaultTable->alias();
$this->_calculateColumnMap($query);
$this->_calculateTypeMap();
$this->_autoFields = $query->autoFields();

if ($this->_useBuffering) {
$count = $this->count();
Expand Down Expand Up @@ -525,7 +533,7 @@ protected function _groupResult($row)
$options['source'] = $target->registryAlias();
unset($presentAliases[$alias]);

if ($assoc['canBeJoined']) {
if ($assoc['canBeJoined'] && $this->_autoFields !== false) {
$hasData = false;
foreach ($results[$alias] as $v) {
if ($v !== null && $v !== []) {
Expand Down
33 changes: 33 additions & 0 deletions tests/TestCase/ORM/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,39 @@ public function testBelongsToEagerLoaderLeavesEmptyAssocation()
$this->assertNotEmpty($comment->comment);
}

/**
* Test showing associated record is preserved when selecting only field with
* null value if auto fields is disabled.
*
* @return void
*/
public function testBelongsToEagerLoaderWithAutoFieldsFalse()
{
$authors = TableRegistry::get('Authors');

$author = $authors->newEntity(['name' => null]);
$authors->save($author);

$articles = TableRegistry::get('Articles');
$articles->belongsTo('Authors');

$article = $articles->newEntity([
'author_id' => $author->id,
'title' => 'article with author with null name'
]);
$articles->save($article);

$result = $articles->find()
->select(['Articles.id', 'Articles.title', 'Authors.name'])
->contain(['Authors'])
->where(['Articles.id' => $article->id])
->autoFields(false)
->hydrate(false)
->first();

$this->assertNotNull($result['author']);
}

/**
* Test that eagerLoader leaves empty associations unpopulated.
*
Expand Down

0 comments on commit e084a14

Please sign in to comment.