/** * inserts node as parent of dest record * * @return bool * @todo Wrap in transaction */ public function insertAsParentOf(Doctrine_Record $dest) { // cannot insert a node that has already has a place within the tree if ($this->isValidNode()) { return false; } // cannot insert as parent of root if ($dest->getNode()->isRoot()) { return false; } // cannot insert as parent of itself if ($dest === $this->record || $dest->exists() && $this->record->exists() && $dest->identifier() === $this->record->identifier()) { throw new Doctrine_Tree_Exception("Cannot insert node as parent of itself"); return false; } $newLeft = $dest->getNode()->getLeftValue(); $newRight = $dest->getNode()->getRightValue() + 2; $newRoot = $dest->getNode()->getRootValue(); $newLevel = $dest->getNode()->getLevel(); $conn = $this->record->getTable()->getConnection(); try { $conn->beginInternalTransaction(); // Make space for new node $this->shiftRLValues($dest->getNode()->getRightValue() + 1, 2, $newRoot); // Slide child nodes over one and down one to allow new parent to wrap them $componentName = $this->_tree->getBaseComponent(); $q = new Doctrine_Query(); $q->update($componentName); $q->set("{$componentName}.lft", "{$componentName}.lft + 1"); $q->set("{$componentName}.rgt", "{$componentName}.rgt + 1"); $q->set("{$componentName}.level", "{$componentName}.level + 1"); $q->where("{$componentName}.lft >= ? AND {$componentName}.rgt <= ?", array($newLeft, $newRight)); $q = $this->_tree->returnQueryWithRootId($q, $newRoot); $q->execute(); $this->record['level'] = $newLevel; $this->insertNode($newLeft, $newRight, $newRoot); $conn->commit(); } catch (Exception $e) { $conn->rollback(); throw $e; } return true; }
/** * inserts node as parent of dest record * * @return bool * @todo Wrap in transaction */ public function insertAsParentOf(Doctrine_Record $dest) { // cannot insert a node that has already has a place within the tree if ($this->isValidNode()) { return false; } // cannot insert as parent of root if ($dest->getNode()->isRoot()) { return false; } $newLeft = $dest->getNode()->getLeftValue(); $newRight = $dest->getNode()->getRightValue() + 2; $newRoot = $dest->getNode()->getRootValue(); $newLevel = $dest->getNode()->getLevel(); // Make space for new node $this->shiftRLValues($dest->getNode()->getRightValue() + 1, 2, $newRoot); // Slide child nodes over one and down one to allow new parent to wrap them $componentName = $this->_tree->getBaseComponent(); $q = new Doctrine_Query(); $q->update($componentName); $q->set("{$componentName}.lft", "{$componentName}.lft + 1"); $q->set("{$componentName}.rgt", "{$componentName}.rgt + 1"); $q->set("{$componentName}.level", "{$componentName}.level + 1"); $q->where("{$componentName}.lft >= ? AND {$componentName}.rgt <= ?", array($newLeft, $newRight)); $q = $this->_tree->returnQueryWithRootId($q, $newRoot); $q->execute(); $this->record['level'] = $newLevel; $this->insertNode($newLeft, $newRight, $newRoot); return true; }