Fixed bug where associated model's afterfind was not triggered in particular case

This commit is contained in:
ADmad 2012-04-04 19:48:37 +05:30
parent 77f698dbcd
commit 93639a7c43
3 changed files with 69 additions and 2 deletions

View file

@ -1113,7 +1113,7 @@ class DboSource extends DataSource {
* @return array Array of results that have been filtered through $model->afterFind
*/
protected function _filterResults(&$results, Model $model, $filtered = array()) {
$current = current($results);
$current = reset($results);
if (!is_array($current)) {
return array();
}

View file

@ -5019,6 +5019,39 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result[0]['Post'][0]['Comment'][0]);
}
/**
* testDeeperAssociationAfterFind method
*
* @return void
*/
public function testDeeperAssociationAfterFind() {
$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article');
$Post = new Post();
$Post->bindModel(array(
'hasMany' => array(
'Comment' => array(
'className' => 'ModifiedComment',
'foreignKey' => 'article_id',
)
)));
$Post->Comment->bindModel(array(
'hasOne' => array(
'Attachment' => array(
'className' => 'ModifiedAttachment',
)
)));
$result = $Post->find('first', array(
'conditions' => array('Post.id' => 2),
'recursive' => 2
));
$this->assertTrue(isset($result['Comment'][0]['callback']));
$this->assertEquals('Fire', $result['Comment'][0]['callback']);
$this->assertTrue(isset($result['Comment'][0]['Attachment']['callback']));
$this->assertEquals('Fired', $result['Comment'][0]['Attachment']['callback']);
}
/**
* Tests that callbacks can be properly disabled
*

View file

@ -675,7 +675,6 @@ class MergeVarPluginComment extends MergeVarPluginAppModel {
public $useTable = 'comments';
}
/**
* Attachment class
*
@ -698,6 +697,41 @@ class Attachment extends CakeTestModel {
public $belongsTo = array('Comment');
}
/**
* ModifiedAttachment class
*
* @package Cake.Test.Case.Model
*/
class ModifiedAttachment extends CakeTestModel {
/**
* name property
*
* @var string 'ModifiedAttachment'
*/
public $name = 'ModifiedAttachment';
/**
* useTable property
*
* @var string 'attachments'
*/
public $useTable = 'attachments';
/**
* afterFind callback
*
* @return void
*/
public function afterFind($results, $primary = false) {
if (isset($results['id'])) {
$results['callback'] = 'Fired';
}
return $results;
}
}
/**
* Category class
*