mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Fixing issue where a beforeDelete() could trigger a table truncation.
Moving the exists check below beforeDelete() and behavior->beforeDelete() so any records deleted in the callbacks will not exist when db->delete() is called. Test updated. Fixes #250
This commit is contained in:
parent
38e128b597
commit
c573fd0432
2 changed files with 10 additions and 5 deletions
|
@ -1803,14 +1803,15 @@ class Model extends Overloadable {
|
|||
}
|
||||
$id = $this->id;
|
||||
|
||||
if ($this->exists() && $this->beforeDelete($cascade)) {
|
||||
$db =& ConnectionManager::getDataSource($this->useDbConfig);
|
||||
if ($this->beforeDelete($cascade)) {
|
||||
$filters = $this->Behaviors->trigger($this, 'beforeDelete', array($cascade), array(
|
||||
'break' => true, 'breakOn' => false
|
||||
));
|
||||
if (!$filters) {
|
||||
if (!$filters || !$this->exists()) {
|
||||
return false;
|
||||
}
|
||||
$db =& ConnectionManager::getDataSource($this->useDbConfig);
|
||||
|
||||
$this->_deleteDependent($id, $cascade);
|
||||
$this->_deleteLinks($id);
|
||||
$this->id = $id;
|
||||
|
|
|
@ -737,14 +737,18 @@ class ModelDeleteTest extends BaseModelTest {
|
|||
$this->assertTrue($result > 1, 'Comments are all gone.');
|
||||
}
|
||||
|
||||
function testBeforeDeleteWipingTable2() {
|
||||
/**
|
||||
* test that deleting the same record from the beforeDelete and the delete doesn't truncate the table.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testBeforeDeleteWipingTableWithDuplicateDelete() {
|
||||
$this->loadFixtures('Comment');
|
||||
|
||||
$Comment =& new BeforeDeleteComment();
|
||||
$Comment->delete(1);
|
||||
|
||||
$result = $Comment->find('count');
|
||||
|
||||
$this->assertTrue($result > 1, 'Comments are all gone.');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue