summaryrefslogtreecommitdiff
path: root/modules/gallery/libraries/ORM_MPTT.php
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2010-01-15 12:15:20 -0800
committerBharat Mediratta <bharat@menalto.com>2010-01-15 12:15:20 -0800
commit50e3cc5837df7b0ae8e2d43a3dacee7500ba6db8 (patch)
tree95697207ad22851d0b1daef946e8869d5679c590 /modules/gallery/libraries/ORM_MPTT.php
parent5809949ae8ff87cd5acf56c528e6dc2af6619513 (diff)
Move model business logic out of album::create() and into
Item_Model::save(). This makes creating albums similar to editing them and makes it difficult to create an album poorly. I expect to be able to remove a lot of code from the photo and movie helper because it's duplicated here. In order to do this, I refactored ORM_MPTT::add_to_parent() into ORM_MPTT::save() so we now add it to the parent when we do save. This allows us to call save() only once which saves a database call per add. The Albums_Controller logic is roughly the same as before. Haven't updated the tests yet, they're going to fail miserably since many of them depend on album::create() which is now gone.
Diffstat (limited to 'modules/gallery/libraries/ORM_MPTT.php')
-rw-r--r--modules/gallery/libraries/ORM_MPTT.php59
1 files changed, 29 insertions, 30 deletions
diff --git a/modules/gallery/libraries/ORM_MPTT.php b/modules/gallery/libraries/ORM_MPTT.php
index ed77cac9..46ae0af8 100644
--- a/modules/gallery/libraries/ORM_MPTT.php
+++ b/modules/gallery/libraries/ORM_MPTT.php
@@ -40,44 +40,43 @@ class ORM_MPTT_Core extends ORM {
}
/**
- * Add this node as a child of the parent provided.
+ * Overload ORM::save() to update the MPTT tree when we add new items to the hierarchy.
*
* @chainable
- * @param integer $parent_id the id of the parent node
- * @return ORM
+ * @return ORM
*/
- function add_to_parent($parent) {
- $this->lock();
- $parent->reload(); // Assume that the prior lock holder may have changed the parent
+ function save() {
+ if (!$this->loaded()) {
+ $this->lock();
+ $parent = ORM::factory("item")->where("id", "=", $this->parent_id)->find();
- try {
- // Make a hole in the parent for this new item
- $this->db_builder
- ->update($this->table_name)
- ->set("left_ptr", new Database_Expression("`left_ptr` + 2"))
- ->where("left_ptr", ">=", $parent->right_ptr)
- ->execute();
- $this->db_builder
- ->update($this->table_name)
- ->set("right_ptr", new Database_Expression("`right_ptr` + 2"))
- ->where("right_ptr", ">=", $parent->right_ptr)
- ->execute();
- $parent->right_ptr += 2;
+ try {
+ // Make a hole in the parent for this new item
+ $this->db_builder
+ ->update($this->table_name)
+ ->set("left_ptr", new Database_Expression("`left_ptr` + 2"))
+ ->where("left_ptr", ">=", $parent->right_ptr)
+ ->execute();
+ $this->db_builder
+ ->update($this->table_name)
+ ->set("right_ptr", new Database_Expression("`right_ptr` + 2"))
+ ->where("right_ptr", ">=", $parent->right_ptr)
+ ->execute();
+ $parent->right_ptr += 2;
- // Insert this item into the hole
- $this->left_ptr = $parent->right_ptr - 2;
- $this->right_ptr = $parent->right_ptr - 1;
- $this->parent_id = $parent->id;
- $this->level = $parent->level + 1;
- $this->save();
- $parent->reload();
- } catch (Exception $e) {
+ // Insert this item into the hole
+ $this->left_ptr = $parent->right_ptr - 2;
+ $this->right_ptr = $parent->right_ptr - 1;
+ $this->parent_id = $parent->id;
+ $this->level = $parent->level + 1;
+ } catch (Exception $e) {
+ $this->unlock();
+ throw $e;
+ }
$this->unlock();
- throw $e;
}
- $this->unlock();
- return $this;
+ return parent::save();
}
/**