Skip to content

Commit

Permalink
Fix different format of $results in afterFind
Browse files Browse the repository at this point in the history
Refs #2529

As of this commit, we can get consistent format of $resutls in afterFind.
And we can keep backward compatibility if Model::$useConsistentAfterFind is set to false.
  • Loading branch information
chinpei215 committed Aug 10, 2014
1 parent c227c14 commit c246695
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -1399,10 +1399,15 @@ public function queryAssociation(Model $Model, Model $LinkModel, $type, $associa
$this->_mergeAssociation($row, $merge, $association, $type);
}
} else {
if (!$prefetched && $LinkModel->useConsistentAfterFind) {
if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
$this->_filterResultsInclusive($assocResultSet, $Model, array($association));
}
}
$this->_mergeAssociation($row, $assocResultSet, $association, $type, $selfJoin);
}

if ($type !== 'hasAndBelongsToMany' && isset($row[$association]) && !$prefetched) {
if ($type !== 'hasAndBelongsToMany' && isset($row[$association]) && !$prefetched && !$LinkModel->useConsistentAfterFind) {
$row[$association] = $LinkModel->afterFind($row[$association], false);
}

Expand Down
19 changes: 19 additions & 0 deletions lib/Cake/Model/Model.php
Expand Up @@ -603,6 +603,25 @@ class Model extends Object implements CakeEventListener {

// @codingStandardsIgnoreEnd

/**
* If true, afterFind will be passed consistent formatted $results in case of $primary is false.
* The format will be such as the following.
*
* {{{
* $results = array(
* 0 => array(
* 'ModelName' => array(
* 'field1' => 'value1',
* 'field2' => 'value2'
* )
* )
* );
* }}}
*
* @var bool
*/
public $useConsistentAfterFind = true;

/**
* The ID of the model record that was last inserted.
*
Expand Down
63 changes: 63 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
Expand Up @@ -1537,4 +1537,67 @@ public function testCountAfterFindCalls() {
$this->assertCount(4, $result['Article'][0]['Comment']);
$this->assertCount(0, $result['Article'][1]['Comment']);
}

/**
* Test format of $results in afterFind
*
* @return void
*/
public function testUseConsistentAfterFind() {
$this->loadFixtures('Author', 'Post');

$expected = array(
'Author' => array(
'id' => '1',
'user' => 'mariano',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31',
'test' => 'working',
),
'Post' => array(
array(
'id' => '1',
'author_id' => '1',
'title' => 'First Post',
'body' => 'First Post Body',
'published' => 'Y',
'created' => '2007-03-18 10:39:23',
'updated' => '2007-03-18 10:41:31',
),
array(
'id' => '3',
'author_id' => '1',
'title' => 'Third Post',
'body' => 'Third Post Body',
'published' => 'Y',
'created' => '2007-03-18 10:43:23',
'updated' => '2007-03-18 10:45:31',
),
),
);

$Author = new Author();
$Post = $this->getMock('Post', array('afterFind'), array(), '', true);
$Post->expects($this->at(0))->method('afterFind')->with(array(array('Post' => $expected['Post'][0])), $this->isFalse())->will($this->returnArgument(0));
$Post->expects($this->at(1))->method('afterFind')->with(array(array('Post' => $expected['Post'][1])), $this->isFalse())->will($this->returnArgument(0));

$Author->bindModel(array('hasMany' => array('Post' => array('limit' => 2, 'order' => 'Post.id'))));
$Author->Post = $Post;

$result = $Author->find('first', array('conditions' => array('Author.id' => 1), 'recursive' => 1));
$this->assertEquals($expected, $result);

// Backward compatiblity
$Author = new Author();
$Post = $this->getMock('Post', array('afterFind'), array(), '', true);
$Post->expects($this->once())->method('afterFind')->with($expected['Post'], $this->isFalse())->will($this->returnArgument(0));
$Post->useConsistentAfterFind = false;

$Author->bindModel(array('hasMany' => array('Post' => array('limit' => 2, 'order' => 'Post.id'))));
$Author->Post = $Post;

$result = $Author->find('first', array('conditions' => array('Author.id' => 1), 'recursive' => 1));
$this->assertEquals($expected, $result);
}
}
10 changes: 8 additions & 2 deletions lib/Cake/Test/Case/Model/models.php
Expand Up @@ -732,8 +732,14 @@ class ModifiedAttachment extends CakeTestModel {
* @return void
*/
public function afterFind($results, $primary = false) {
if (isset($results['id'])) {
$results['callback'] = 'Fired';
if ($this->useConsistentAfterFind) {
if (isset($results[0][$this->alias]['id'])) {
$results[0][$this->alias]['callback'] = 'Fired';
}
} else {
if (isset($results['id'])) {
$results['callback'] = 'Fired';
}
}
return $results;
}
Expand Down

0 comments on commit c246695

Please sign in to comment.