/**
  * @param int $to
  * @param int $depth
  * @throws Exception
  */
 protected function moveNode($to, $depth = 0)
 {
     $left = $this->owner->getAttribute($this->leftAttribute);
     $right = $this->owner->getAttribute($this->rightAttribute);
     $depth = $this->owner->getAttribute($this->depthAttribute) - $this->node->getAttribute($this->depthAttribute) - $depth;
     if ($this->treeAttribute === null || $this->owner->getAttribute($this->treeAttribute) === $this->node->getAttribute($this->treeAttribute)) {
         // same root
         $this->owner->updateAll([$this->depthAttribute => new Expression("-[[{$this->depthAttribute}]]" . sprintf('%+d', $depth))], $this->getDescendants(null, true)->where);
         $delta = $right - $left + 1;
         if ($left >= $to) {
             $this->shift($to, $left - 1, $delta);
             $delta = $to - $left;
         } else {
             $this->shift($right + 1, $to, -$delta);
             $delta = $to - $right;
         }
         $this->owner->updateAll([$this->leftAttribute => new Expression("[[{$this->leftAttribute}]]" . sprintf('%+d', $delta)), $this->rightAttribute => new Expression("[[{$this->rightAttribute}]]" . sprintf('%+d', $delta)), $this->depthAttribute => new Expression("-[[{$this->depthAttribute}]]")], ['and', $this->getDescendants(null, true)->where, ['<', $this->depthAttribute, 0]]);
     } else {
         // move from other root
         $tree = $this->node->getAttribute($this->treeAttribute);
         $this->shift($to, null, $right - $left + 1, $tree);
         $delta = $to - $left;
         $this->owner->updateAll([$this->leftAttribute => new Expression("[[{$this->leftAttribute}]]" . sprintf('%+d', $delta)), $this->rightAttribute => new Expression("[[{$this->rightAttribute}]]" . sprintf('%+d', $delta)), $this->depthAttribute => new Expression("[[{$this->depthAttribute}]]" . sprintf('%+d', -$depth)), $this->treeAttribute => $tree], $this->getDescendants(null, true)->where);
         $this->shift($right + 1, null, $left - $right - 1);
     }
 }
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [['class' => NestedSetsBehavior::className(), 'treeAttribute' => 'tree']];
 }
Example #3
0
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [['class' => AdjacencyListBehavior::className(), 'sortable' => false], ['class' => NestedSetsBehavior::className(), 'treeAttribute' => 'tree']];
 }
Example #4
0
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [['class' => NestedSetsBehavior::className()]];
 }
Example #5
0
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return [TimestampBehavior::className(), ['class' => AdjacencyListBehavior::className(), 'parentAttribute' => 'parent_id'], ['class' => NestedSetsBehavior::className(), 'treeAttribute' => null, 'leftAttribute' => 'lft', 'rightAttribute' => 'rgt', 'depthAttribute' => 'level']];
 }