Baum\Node::setDepthWithSubtree PHP Method

setDepthWithSubtree() public method

Sets the depth attribute for the current node and all of its descendants.
public setDepthWithSubtree ( ) : Node
return Node
    public function setDepthWithSubtree()
    {
        $self = $this;
        $this->getConnection()->transaction(function () use($self) {
            $self->reload();
            $self->descendantsAndSelf()->select($self->getKeyName())->lockForUpdate()->get();
            $oldDepth = !is_null($self->getDepth()) ? $self->getDepth() : 0;
            $newDepth = $self->getLevel();
            $self->newNestedSetQuery()->where($self->getKeyName(), '=', $self->getKey())->update([$self->getDepthColumnName() => $newDepth]);
            $self->setAttribute($self->getDepthColumnName(), $newDepth);
            $diff = $newDepth - $oldDepth;
            if (!$self->isLeaf() && $diff != 0) {
                $self->descendants()->increment($self->getDepthColumnName(), $diff);
            }
        });
        return $this;
    }

Usage Example

Esempio n. 1
0
File: Move.php Progetto: yajra/baum
 /**
  * Perform the move operation.
  *
  * @return \Baum\Node
  */
 public function perform()
 {
     $this->guardAgainstImpossibleMove();
     if ($this->fireMoveEvent('moving') === false) {
         return $this->node;
     }
     if ($this->hasChange()) {
         $self = $this;
         $this->node->getConnection()->transaction(function () use($self) {
             $self->updateStructure();
         });
         $this->target->reload();
         $this->node->setDepthWithSubtree();
         $this->node->reload();
     }
     $this->fireMoveEvent('moved', false);
     return $this->node;
 }