Skip to content
Permalink
Browse files

hasOne/belongsTo associations should contain associated records in af…

…terFind

Before 1fe943d,
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 1fe943d,
it doesn't work if associated records are used in afterFind.
This commit fixes it.
  • Loading branch information...
chinpei215 committed Aug 27, 2014
1 parent 5863add commit b74774bb0c4a382540087d8e68e7a854eadd52be
Showing with 55 additions and 5 deletions.
  1. +0 −5 lib/Cake/Model/Datasource/DboSource.php
  2. +55 −0 lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
@@ -1113,11 +1113,6 @@ public function read(Model $Model, $queryData = array(), $recursive = null) {
$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();
@@ -1508,4 +1508,59 @@ public function testCountAfterFindCalls() {
$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);

This comment has been minimized.

Copy link
@karan-sharma

karan-sharma Oct 13, 2014

Please use the null instead of ''

This comment has been minimized.

Copy link
@chinpei215

chinpei215 Oct 13, 2014

Author Member

I can fix it if needed.
Please tell me the reason.
The default value is ''.

This comment has been minimized.

Copy link
@karan-sharma

karan-sharma Oct 13, 2014

if use '' then it take one bit memory space

This comment has been minimized.

Copy link
@ADmad

This comment has been minimized.

Copy link
@josegonzalez

josegonzalez Oct 13, 2014

Member

@karan-sharma first off, that's a silly optimization. Second, if you pass a non-string value to the fourth argument for the getMock call, PHPUnit will throw an PHPUnit_Framework_Exception. You must pass an empty string.

$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));
}
}

0 comments on commit b74774b

Please sign in to comment.
You can’t perform that action at this time.