summaryrefslogtreecommitdiff
path: root/core/libraries
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-12-04 05:47:05 +0000
committerBharat Mediratta <bharat@menalto.com>2008-12-04 05:47:05 +0000
commit375e7c675ef31c9b3c44292f1577fb5f1cea42a2 (patch)
tree373f2820c669a2c5bf2f7fe51ed9d3e39edfe208 /core/libraries
parent89a346c1e14be26a9b7ac98c6996e84b537228ea (diff)
Implement ORM_MPTT::delete() properly.
Inline ORM_MPTT::_grow() for now Rewrite ORM_MPTT tests to be domain specific; they no longer use album/photo helpers.
Diffstat (limited to 'core/libraries')
-rw-r--r--core/libraries/ORM_MPTT.php47
1 files changed, 31 insertions, 16 deletions
diff --git a/core/libraries/ORM_MPTT.php b/core/libraries/ORM_MPTT.php
index 2cdc617d..bac4da92 100644
--- a/core/libraries/ORM_MPTT.php
+++ b/core/libraries/ORM_MPTT.php
@@ -56,8 +56,15 @@ class ORM_MPTT_Core extends ORM {
$this->_lock();
try {
+ // Make a hole in the parent for this new item
$parent = ORM::factory($this->model_name, $parent_id);
- $parent->_grow();
+ $this->db->query(
+ "UPDATE `{$this->table_name}` SET `left` = `left` + 2 WHERE `left` >= {$parent->right}");
+ $this->db->query(
+ "UPDATE `{$this->table_name}` SET `right` = `right` + 2 WHERE `right` >= {$parent->right}");
+ $parent->right += 2;
+
+ // Insert this item into the hole
$this->left = $parent->right - 2;
$this->right = $parent->right - 1;
$this->parent_id = $parent->id;
@@ -73,11 +80,32 @@ class ORM_MPTT_Core extends ORM {
}
/**
- * Deleted this node and adjust the left and right pointers
+ * Delete this node and all of its children.
*/
public function delete() {
+ $children = $this->children();
+ if ($children) {
+ foreach ($this->children() as $item) {
+ $item->delete();
+ }
+
+ // Deleting children has affected this item
+ $this->reload();
+ }
+
+ $this->_lock();
+ try {
+ $this->db->query(
+ "UPDATE `{$this->table_name}` SET `left` = `left` - 2 WHERE `left` >= {$this->right}");
+ $this->db->query(
+ "UPDATE `{$this->table_name}` SET `right` = `right` - 2 WHERE `right` >= {$this->right}");
+ } catch (Exception $e) {
+ $this->_unlock();
+ throw $e;
+ }
+
+ $this->_unlock();
parent::delete();
- $this->_grow(-1);
}
/**
@@ -197,19 +225,6 @@ class ORM_MPTT_Core extends ORM {
return parent::reload();
}
- /**
- * Grow this node's space enough to make room for 1 or more new nodes.
- *
- * @param integer $count the number of new nodes to add
- */
- public function _grow($count=1) {
- $size = $count * 2;
- $this->db->query(
- "UPDATE `{$this->table_name}` SET `left` = `left` + $size WHERE `left` >= {$this->right}");
- $this->db->query(
- "UPDATE `{$this->table_name}` SET `right` = `right` + $size WHERE `right` >= {$this->right}");
- $this->right += 2;
- }
/**
* Lock the tree to prevent concurrent modification.