diff --git a/src/ORM/ResultSet.php b/src/ORM/ResultSet.php index b161e946508..edd19901f58 100644 --- a/src/ORM/ResultSet.php +++ b/src/ORM/ResultSet.php @@ -410,11 +410,11 @@ protected function _groupResult($row) { } if (!$hasData) { - continue; + $results[$alias] = null; } } - if ($this->_hydrate && $assoc['canBeJoined']) { + if ($this->_hydrate && $results[$alias] !== null && $assoc['canBeJoined']) { $entity = new $assoc['entityClass']($results[$alias], $options); $entity->clean(); $results[$alias] = $entity; diff --git a/tests/TestCase/ORM/Association/BelongsToTest.php b/tests/TestCase/ORM/Association/BelongsToTest.php index 7274db33588..57becd34dc7 100644 --- a/tests/TestCase/ORM/Association/BelongsToTest.php +++ b/tests/TestCase/ORM/Association/BelongsToTest.php @@ -446,22 +446,4 @@ public function testAttachToBeforeFindExtraOptions() { }]); } -/** - * Test that eagerLoader leaves empty associations unpopulated. - * - * @return void - */ - public function testEagerLoaderLeavesEmptyAssocation() { - $this->loadFixtures('Article', 'Comment'); - $comments = TableRegistry::get('Comments'); - $comments->belongsTo('Articles'); - - // Clear the articles table so we can trigger an empty belongsTo - $articles = TableRegistry::get('Articles'); - $articles->deleteAll([]); - - $comment = $comments->get(1, ['contain' => ['Articles']]); - $this->assertNull($comment->article); - } - } diff --git a/tests/TestCase/ORM/Association/HasOneTest.php b/tests/TestCase/ORM/Association/HasOneTest.php index e9ad1b2a958..bb03ea1effe 100644 --- a/tests/TestCase/ORM/Association/HasOneTest.php +++ b/tests/TestCase/ORM/Association/HasOneTest.php @@ -29,20 +29,6 @@ */ class HasOneTest extends \Cake\TestSuite\TestCase { -/** - * Fixtures to use. - * - * @var array - */ - public $fixtures = ['core.article', 'core.comment']; - -/** - * Don't autoload fixtures as most tests uses mocks. - * - * @var bool - */ - public $autoFixture = false; - /** * Set up * @@ -392,22 +378,4 @@ public function testAttachToBeforeFindExtraOptions() { }]); } -/** - * Test that eagerLoader leaves empty associations unpopulated. - * - * @return void - */ - public function testEagerLoaderLeavesEmptyAssocation() { - $this->loadFixtures('Article', 'Comment'); - $articles = TableRegistry::get('Articles'); - $articles->hasOne('Comments'); - - // Clear the comments table so we can trigger an empty hasOne. - $comments = TableRegistry::get('Comments'); - $comments->deleteAll([]); - - $article = $articles->get(1, ['contain' => ['Comments']]); - $this->assertNull($article->comment); - } - } diff --git a/tests/TestCase/ORM/ResultSetTest.php b/tests/TestCase/ORM/ResultSetTest.php index e31eb1c7df7..85df185ebe8 100644 --- a/tests/TestCase/ORM/ResultSetTest.php +++ b/tests/TestCase/ORM/ResultSetTest.php @@ -20,6 +20,7 @@ use Cake\ORM\Query; use Cake\ORM\ResultSet; use Cake\ORM\Table; +use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; /** @@ -27,7 +28,7 @@ */ class ResultSetTest extends TestCase { - public $fixtures = ['core.article']; + public $fixtures = ['core.article', 'core.comment']; /** * setup @@ -249,4 +250,56 @@ public function testDebugInfo() { $this->assertSame($expected, $results->__debugInfo()); } +/** + * Test that eagerLoader leaves empty associations unpopulated. + * + * @return void + */ + public function testBelongsToEagerLoaderLeavesEmptyAssocation() { + $comments = TableRegistry::get('Comments'); + $comments->belongsTo('Articles'); + + // Clear the articles table so we can trigger an empty belongsTo + $this->table->deleteAll([]); + + $comment = $comments->find()->where(['Comments.id' => 1]) + ->contain(['Articles']) + ->hydrate(false) + ->first(); + $this->assertEquals(1, $comment['id']); + $this->assertNotEmpty($comment['comment']); + $this->assertNull($comment['article']); + + $comment = $comments->get(1, ['contain' => ['Articles']]); + $this->assertNull($comment->article); + $this->assertEquals(1, $comment->id); + $this->assertNotEmpty($comment->comment); + } + +/** + * Test that eagerLoader leaves empty associations unpopulated. + * + * @return void + */ + public function testHasOneEagerLoaderLeavesEmptyAssocation() { + $this->table->hasOne('Comments'); + + // Clear the comments table so we can trigger an empty hasOne. + $comments = TableRegistry::get('Comments'); + $comments->deleteAll([]); + + $article = $this->table->get(1, ['contain' => ['Comments']]); + $this->assertNull($article->comment); + $this->assertEquals(1, $article->id); + $this->assertNotEmpty($article->title); + + $article = $this->table->find()->where(['Articles.id' => 1]) + ->contain(['Comments']) + ->hydrate(false) + ->first(); + $this->assertNull($article['comment']); + $this->assertEquals(1, $article['id']); + $this->assertNotEmpty($article['title']); + } + }