mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
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
This commit is contained in:
parent
773666ddad
commit
7790bcacff
3 changed files with 37 additions and 1 deletions
|
@ -1153,10 +1153,12 @@ class DboSource extends DataSource {
|
|||
}
|
||||
$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 @@ class ModelReadTest extends BaseModelTest {
|
|||
$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 @@ class ModifiedComment extends CakeTestModel {
|
|||
if (isset($results[0])) {
|
||||
$results[0]['Comment']['callback'] = 'Fire';
|
||||
}
|
||||
if ($this->remove) {
|
||||
return array();
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue