public function moveAsRoot()
{
$owner = $this->getOwner();
if (!$this->hasManyRoots) {
throw new Exception('Many roots mode is off.');
}
if ($this->getIsNewRecord()) {
throw new Exception('The node should not be new record.');
}
if ($this->getIsDeletedRecord()) {
throw new Exception('The node should not be deleted.');
}
if ($owner->isRoot()) {
throw new Exception('The node already is root node.');
}
$this->db->begin();
$left = $owner->{$this->leftAttribute};
$right = $owner->{$this->rightAttribute};
$levelDelta = 1 - $owner->{$this->levelAttribute};
$delta = 1 - $left;
$condition = $this->leftAttribute . '>=' . $left . ' AND ';
$condition .= $this->rightAttribute . '<=' . $right . ' AND ';
$condition .= $this->rootAttribute . '=' . $owner->{$this->rootAttribute};
$this->ignoreEvent = true;
foreach ($owner::find($condition) as $i) {
$arr = [$this->leftAttribute => $i->{$this->leftAttribute} + $delta, $this->rightAttribute => $i->{$this->rightAttribute} + $delta, $this->levelAttribute => $i->{$this->levelAttribute} + $levelDelta, $this->rootAttribute => $owner->{$this->primaryKey}];
if ($i->update($arr) == false) {
$this->db->rollback();
$this->ignoreEvent = false;
return false;
}
}
$this->ignoreEvent = false;
$this->shiftLeftRight($right + 1, $left - $right - 1);
$this->db->commit();
return true;
}