From 93639a7c431480bcb6e45f044fb93fc744c852b8 Mon Sep 17 00:00:00 2001 From: ADmad Date: Wed, 4 Apr 2012 19:48:37 +0530 Subject: [PATCH] Fixed bug where associated model's afterfind was not triggered in particular case --- lib/Cake/Model/Datasource/DboSource.php | 2 +- lib/Cake/Test/Case/Model/ModelReadTest.php | 33 ++++++++++++++++++++ lib/Cake/Test/Case/Model/models.php | 36 +++++++++++++++++++++- 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 992dd34a0..543f155e7 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -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(); } diff --git a/lib/Cake/Test/Case/Model/ModelReadTest.php b/lib/Cake/Test/Case/Model/ModelReadTest.php index df45ce514..b0a073086 100644 --- a/lib/Cake/Test/Case/Model/ModelReadTest.php +++ b/lib/Cake/Test/Case/Model/ModelReadTest.php @@ -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 * diff --git a/lib/Cake/Test/Case/Model/models.php b/lib/Cake/Test/Case/Model/models.php index a11bc74eb..a206b0790 100644 --- a/lib/Cake/Test/Case/Model/models.php +++ b/lib/Cake/Test/Case/Model/models.php @@ -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 *