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);
    }