Permalink
Browse files

Fixed bug where associated model's afterfind was not triggered in par…

…ticular case
  • Loading branch information...
1 parent 77f698d commit 93639a7c431480bcb6e45f044fb93fc744c852b8 @ADmad ADmad committed Apr 4, 2012
@@ -1113,7 +1113,7 @@ public function read(Model $model, $queryData = array(), $recursive = null) {
* @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();
}
@@ -5020,6 +5020,39 @@ public function testAssociationAfterFind() {
}
/**
+ * 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
*
* @return void
@@ -675,7 +675,6 @@ class MergeVarPluginComment extends MergeVarPluginAppModel {
public $useTable = 'comments';
}
-
/**
* Attachment class
*
@@ -699,6 +698,41 @@ class Attachment extends CakeTestModel {
}
/**
+ * 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
*
* @package Cake.Test.Case.Model

0 comments on commit 93639a7

Please sign in to comment.