From 375e7c675ef31c9b3c44292f1577fb5f1cea42a2 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 4 Dec 2008 05:47:05 +0000 Subject: 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. --- core/libraries/ORM_MPTT.php | 47 ++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 16 deletions(-) (limited to 'core/libraries') 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. -- cgit v1.2.3