Esempio n. 1
0
 /**
  * @param  \Phalcon\Mvc\ModelInterface $target
  * @param  int $key
  * @param  int $levelUp
  * @param  array $attributes
  *
  * @return boolean
  * @throws PMMException
  */
 private function addNode($target, $key, $levelUp, $attributes)
 {
     $owner = $this->getOwner();
     if (!$target) {
         throw new PMMException('The node cannot be inserted because target is not defined.');
     }
     if (!$this->getIsNewRecord()) {
         throw new PMMException('The node cannot be inserted because it is not new.');
     }
     if ($this->getIsDeletedRecord()) {
         throw new PMMException('The node cannot be inserted because it is deleted.');
     }
     if ($target->getIsDeletedRecord()) {
         throw new PMMException('The node cannot be inserted because target node is deleted.');
     }
     if ($owner == $target) {
         throw new PMMException('The target node should not be self.');
     }
     if (!$levelUp && $target->isRoot()) {
         throw new PMMException('The target node should not be root.');
     }
     if ($this->hasManyRoots) {
         $owner->{$this->rootAttribute} = $target->{$this->rootAttribute};
     }
     $this->shiftLeftRight($key, 2);
     $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;
     return $result;
 }
Esempio n. 2
0
 /**
  * @param  ModelInterface $target
  * @param  int $key
  * @param  int $levelUp
  * @param  array $attributes
  *
  * @return boolean
  * @throws \Exception
  */
 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;
 }