Permalink
Browse files

Allow afterFind() to fully remove an associated record.

By returnning array() or unsetting the 0'th result an afterFind
callback should be able to fully remove data from the results.

Fixes #3541
  • Loading branch information...
1 parent 773666d commit 7790bcacffa8ad33bb94e4fad0f8b9f092e3ffb9 @markstory markstory committed Jan 18, 2013
@@ -1153,10 +1153,12 @@ protected function _filterResults(&$results, Model $model, $filtered = array())
}
$linkedModel = $model->{$className};
$filtering[] = $className;
- foreach ($results as &$result) {
+ foreach ($results as $key => &$result) {
$data = $linkedModel->afterFind(array(array($className => $result[$className])), false);
if (isset($data[0][$className])) {
$result[$className] = $data[0][$className];
+ } else {
+ unset($results[$key]);
}
}
}
@@ -3007,6 +3007,30 @@ public function testSelfAssociationAfterFind() {
}
/**
+ * Test that afterFind can completely unset data.
+ *
+ * @return void
+ */
+ public function testAfterFindUnset() {
+ $this->loadFixtures('Article', 'Comment', 'User');
+ $model = new CustomArticle();
+ $model->bindModel(array(
+ 'hasMany' => array(
+ 'ModifiedComment' => array(
+ 'className' => 'ModifiedComment',
+ 'foreignKey' => 'article_id',
+ )
+ )
+ ));
+ $model->ModifiedComment->remove = true;
+ $result = $model->find('all');
+ $this->assertTrue(
+ empty($result[0]['ModifiedComment']),
+ 'Zeroith row should be removed by afterFind'
+ );
+ }
+
+/**
* testFindThreadedNoParent method
*
* @return void
@@ -552,6 +552,13 @@ class ModifiedComment extends CakeTestModel {
public $useTable = 'comments';
/**
+ * Property used to toggle filtering of results
+ *
+ * @var boolean
+ */
+ public $remove = false;
+
+/**
* belongsTo property
*
* @var array
@@ -567,6 +574,9 @@ public function afterFind($results, $primary = false) {
if (isset($results[0])) {
$results[0]['Comment']['callback'] = 'Fire';
}
+ if ($this->remove) {
+ return array();
+ }
return $results;
}

0 comments on commit 7790bca

Please sign in to comment.