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:
mark_story 2010-09-29 23:31:41 -04:00
parent 38e128b597
commit c573fd0432
2 changed files with 10 additions and 5 deletions

View file

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

View file

@ -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.');
}
}