Skip to content
Browse files

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
  • Loading branch information...
1 parent 8d2d83c commit 16fc261f8f6be1b2ba2b0e9e1a6cc480f86af1d0 @markstory markstory committed
Showing with 14 additions and 5 deletions.
  1. +8 −3 cake/libs/model/model.php
  2. +6 −2 cake/tests/cases/libs/model/model_delete.test.php
View
11 cake/libs/model/model.php
@@ -1746,11 +1746,16 @@ function del($id = null, $cascade = true) {
}
$id = $this->id;
- if ($this->exists() && $this->beforeDelete($cascade)) {
- $db =& ConnectionManager::getDataSource($this->useDbConfig);
- if (!$this->Behaviors->trigger($this, 'beforeDelete', array($cascade), array('break' => true, 'breakOn' => false))) {
+
+ if ($this->beforeDelete($cascade)) {
+ $filters = $this->Behaviors->trigger($this, 'beforeDelete', array($cascade), array(
+ 'break' => true, 'breakOn' => false
+ ));
+ if (!$filters || !$this->exists()) {
return false;
}
+ $db =& ConnectionManager::getDataSource($this->useDbConfig);
+
$this->_deleteDependent($id, $cascade);
$this->_deleteLinks($id);
$this->id = $id;
View
8 cake/tests/cases/libs/model/model_delete.test.php
@@ -664,14 +664,18 @@ function testBeforeDeleteWipingTable() {
$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.');
}
}

0 comments on commit 16fc261

Please sign in to comment.
Something went wrong with that request. Please try again.