From b74774bb0c4a382540087d8e68e7a854eadd52be Mon Sep 17 00:00:00 2001 From: chinpei215 Date: Wed, 27 Aug 2014 09:32:00 +0900 Subject: [PATCH] hasOne/belongsTo associations should contain associated records in afterFind Before 1fe943d6f1814cd3756d71c035d2b69fc36ce066, afterFind() is called twice with belongsTo/hasOne associations. Although $results also doesn't contain associated records on first time, it contains them on second time. After 1fe943d6f1814cd3756d71c035d2b69fc36ce066, it doesn't work if associated records are used in afterFind. This commit fixes it. --- lib/Cake/Model/Datasource/DboSource.php | 5 -- .../Case/Model/Datasource/DboSourceTest.php | 55 +++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 1a894b55b..6bc5780b3 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -1113,11 +1113,6 @@ class DboSource extends DataSource { $filtered = array(); - // Filter hasOne and belongsTo associations - if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') { - $filtered = $this->_filterResults($resultSet, $Model); - } - // Deep associations if ($Model->recursive > -1) { $joined = array(); diff --git a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php index 25439c8ef..ffc48c765 100644 --- a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php @@ -1508,4 +1508,59 @@ class DboSourceTest extends CakeTestCase { $this->assertCount(2, $result['Article']['Tag']); $this->assertCount(2, $result['Article']['Comment']); } + +/** + * Test that afterFind is called correctly for 'hasOne' association. + * + * @return void + */ + public function testHasOneAfterFind() { + $this->loadFixtures('Article', 'User', 'Comment'); + + $User = new User(); + $User->bindModel(array('hasOne' => array('Article'))); + + $Article = $this->getMock('Article', array('afterFind'), array(), '', true); + $Article->unbindModel(array( + 'belongsTo' => array('User'), + 'hasMany' => array('Comment'), + 'hasAndBelongsToMany' => array('Tag') + )); + $Article->bindModel(array( + 'hasOne' => array('Comment'), + )); + $Article->expects($this->once()) + ->method('afterFind') + ->with( + $this->equalTo( + array( + 0 => array( + 'Article' => array( + 'id' => '1', + 'user_id' => '1', + 'title' => 'First Article', + 'body' => 'First Article Body', + 'published' => 'Y', + 'created' => '2007-03-18 10:39:23', + 'updated' => '2007-03-18 10:41:31', + 'Comment' => array( + 'id' => '1', + 'article_id' => '1', + 'user_id' => '2', + 'comment' => 'First Comment for First Article', + 'published' => 'Y', + 'created' => '2007-03-18 10:45:23', + 'updated' => '2007-03-18 10:47:31', + ) + ) + ) + ) + ), + $this->isFalse() + ) + ->will($this->returnArgument(0)); + + $User->Article = $Article; + $User->find('first', array('conditions' => array('User.id' => 1), 'recursive' => 2)); + } }