public function save(array $fields = [])
    {
        if ($this->getIsNewRecord()) {
            if ($this->parent) {
                $this->appendTo($this->parent);
            } else {
                $this->makeRoot();
            }

            return parent::save($fields);
        }

        if (in_array('parent_id', $this->getDirtyAttributes())) {
            $saved = parent::save($fields);
            if ($saved) {
                if ($this->parent) {
                    $this->moveAsLast($this->parent);
                } elseif ($this->isRoot()) {
                    $this->moveAsRoot();
                }

                /** @var array $parent */
                $parent = $this->objects()->asArray()->get(['pk' => $this->pk]);
                if (null !== $parent) {
                    $this->setAttributes($parent);
                    $this->setIsNewRecord(false);
                }

                return $saved;
            }

            return $saved;
        }

        return parent::save($fields);
    }
public function save(array $fields = [])
    {
        if ($this->getIsNewRecord()) {
            if ($this->parent) {
                $this->appendTo($this->parent);
            } else {
                $this->makeRoot();
            }

            return parent::save($fields);
        }

        if (in_array('parent_id', $this->getDirtyAttributes())) {
            $saved = parent::save($fields);
            if ($saved) {
                if ($this->parent) {
                    $this->moveAsLast($this->parent);
                } elseif ($this->isRoot()) {
                    $this->moveAsRoot();
                }

                /** @var array $parent */
                $parent = $this->objects()->asArray()->get(['pk' => $this->pk]);
                if (null !== $parent) {
                    $this->setAttributes($parent);
                    $this->setIsNewRecord(false);
                }

                return $saved;
            }

            return $saved;
        }

        return parent::save($fields);
    }
public function save ( array $ fields = [ ] ) { if ( $ this -> getIsNewRecord ( ) ) { if ( $ this -> parent ) { $ this -> appendTo ( $ this -> parent ) ; } else { $ this -> makeRoot ( ) ; } return parent :: save ( $ fields ) ; } if ( in_array ( 'parent_id' , $ this -> getDirtyAttributes ( ) ) ) { $ saved = parent :: save ( $ fields ) ; if ( $ saved ) { if ( $ this -> parent ) { $ this -> moveAsLast ( $ this -> parent ) ; } elseif ( $ this -> isRoot ( ) ) { $ this -> moveAsRoot ( ) ; } /** @var array $parent */ $ parent = $ this -> objects ( ) -> asArray ( ) -> get ( [ 'pk' => $ this -> pk ] ) ; if ( null !== $ parent ) { $ this -> setAttributes ( $ parent ) ; $ this -> setIsNewRecord ( false ) ; } return $ saved ; } return $ saved ; } return parent :: save ( $ fields ) ; }
Create root node if multiple-root tree mode. Update node if it's not new.

@param array $fields

@throws \Exception

@return bool whether the saving succeeds
Create root node if multiple - root tree mode . Update node if it s not new .