Commit f0236dc9 authored by romanb's avatar romanb

More additions to the NestedSet implementation: Added $node->makeRoot() which...

More additions to the NestedSet implementation: Added $node->makeRoot() which can be used when using many roots to detach a node from a tree and make it a root node itself.
parent e90e62c9
...@@ -539,6 +539,59 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int ...@@ -539,6 +539,59 @@ class Doctrine_Node_NestedSet extends Doctrine_Node implements Doctrine_Node_Int
} }
} }
/**
* Enter description here...
*
* @todo Exception handling/wrapping
*/
public function makeRoot()
{
// TODO: throw exception instead?
if ($this->getLeftValue() == 1 || !$this->record->getTable()->getTree()->getAttribute('hasManyRoots')) {
return false;
}
$oldRgt = $this->getRightValue();
$oldLft = $this->getLeftValue();
$oldRoot = $this->getRootValue();
try {
$conn = $this->record->getTable()->getConnection();
$conn->beginTransaction();
// Detach from old tree
$first = $oldRgt + 1;
$delta = $oldLft - $oldRgt - 1;
$this->shiftRLValues($first, $delta, $this->getRootValue());
// Set new lft/rgt/root values for root node
$this->setLeftValue(1);
$this->setRightValue($oldRgt - $oldLft + 1);
$this->setRootValue($this->record->id);
// Update descendants lft/rgt/root values
$diff = 1 - $oldLft;
$newRoot = $this->record->id;
$componentName = $this->record->getTable()->getComponentName();
$rootColName = $this->record->getTable()->getTree()->getAttribute('rootColumnName');
$q = $this->record->getTable()->createQuery();
$q = $q->update($componentName)
->set($componentName . '.lft', 'lft + ' . $diff)
->set($componentName . '.rgt', 'rgt + ' . $diff)
->set($componentName . '.' . $rootColName, $newRoot)
->where($componentName . '.lft > ? AND ' . $componentName . '.rgt < ?',
array($oldLft, $oldRgt));
$q = $this->record->getTable()->getTree()->returnQueryWithRootId($q, $oldRoot);
$q->execute();
$conn->commit();
}
catch (Exception $e) {
$conn->rollback();
throw $e;
}
}
/** /**
* adds node as last child of record * adds node as last child of record
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment