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...
markstory committed Jan 18, 2013
1 parent 773666d commit 7790bcacffa8ad33bb94e4fad0f8b9f092e3ffb9
@@ -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]);
}
}
}
@@ -3006,6 +3006,30 @@ public function testSelfAssociationAfterFind() {
$this->assertEquals($afterFindData, $noAfterFindData);
}
/**
* 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
*
@@ -551,6 +551,13 @@ class ModifiedComment extends CakeTestModel {
*/
public $useTable = 'comments';
/**
* Property used to toggle filtering of results
*
* @var boolean
*/
public $remove = false;
/**
* belongsTo property
*
@@ -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.