private function addNode(ModelInterface $target, $key, $levelUp, array $attributes = null)
{
$owner = $this->getOwner();
if (!$this->getIsNewRecord()) {
throw new Exception('The node cannot be inserted because it is not new.');
}
if ($this->getIsDeletedRecord()) {
throw new Exception('The node cannot be inserted because it is deleted.');
}
if ($target->getIsDeletedRecord()) {
throw new Exception('The node cannot be inserted because target node is deleted.');
}
if ($owner == $target) {
throw new Exception('The target node should not be self.');
}
if (!$levelUp && $target->isRoot()) {
throw new Exception('The target node should not be root.');
}
if ($this->hasManyRoots) {
$owner->{$this->rootAttribute} = $target->{$this->rootAttribute};
}
$db = $this->getDbHandler($owner);
$db->begin();
try {
$this->ignoreEvent = true;
$this->shiftLeftRight($key, 2);
$this->ignoreEvent = false;
$owner->{$this->leftAttribute} = $key;
$owner->{$this->rightAttribute} = $key + 1;
$owner->{$this->levelAttribute} = $target->{$this->levelAttribute} + $levelUp;
$this->ignoreEvent = true;
$result = $owner->create($attributes);
$this->ignoreEvent = false;
if (!$result) {
$db->rollback();
$this->ignoreEvent = false;
return false;
}
$db->commit();
} catch (\Exception $e) {
$db->rollback();
$this->ignoreEvent = false;
throw $e;
}
return true;
}