Browse Source

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

ADmad 14 years ago
parent
commit
93639a7c43

+ 1 - 1
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();
 		}

+ 33 - 0
lib/Cake/Test/Case/Model/ModelReadTest.php

@@ -5020,6 +5020,39 @@ class ModelReadTest extends BaseModelTest {
 	}
 
 /**
+ * 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

+ 35 - 1
lib/Cake/Test/Case/Model/models.php

@@ -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