Подготовка модели к вставке в дерево в качестве первого потомка заданного элемента
@param \T4\Orm\Model $model
@param \T4\Orm\Model $parent
@throws Exception
503812
protected function insertModelAsFirstChildOf(Model &$model, Model &$parent)
    {
        if ($parent->isNew())
            throw new Exception('Parent model should be saved before child insert');

        /** @var \T4\Orm\Model $class */
        $class = get_class($model);
        $tableName = $class::getTableName();
        /** @var \T4\Dbal\Connection $connection */
        $connection = $class::getDbConnection();

        $parent->refreshTreeColumns();
        $width = $model->getTreeWidth();
        $this->expandTreeInElementFirst($parent, $width);
        if (!$model->isNew()) {
            $model->refreshTreeColumns();
        }

        $diff = $parent->__lft - $model->__lft + 1;
        $lvlDiff = $parent->__lvl - $model->__lvl + 1;

        if (!$model->isNew()) {

            $query = (new Query())
                ->update()
                ->table($tableName)
                ->values([
                    '__lft' => '__lft + :diff',
                    '__rgt' => '__rgt + :diff',
                    '__lvl' => '__lvl + :lvldiff',
                    '__prt' => 'CASE WHEN __id=:id THEN :parentid ELSE __prt END',
                ])
                ->where('__lft>=:lft AND __rgt<=:rgt')
                ->params([':lft'=>$model->__lft, ':rgt'=>$model->__rgt, ':diff'=>$diff, ':lvldiff'=>$lvlDiff, ':id'=>$model->getPk(), ':parentid'=>$parent->getPk()]);
            $connection->execute($query);

            $this->removeFromTreeByElement($model);

        }

        $model->__lft = $parent->__lft + 1;
        $model->__rgt = $parent->__lft + $width + 1;
        $model->__lvl = $parent->__lvl + 1;
        $model->__prt = $parent->getPk();
    }
354296
public function save(Storable $model, AbstractModel $parent = null)
    {
        $response = $this->request(
            $this->buildUrl($model, $parent), $model->getId() > 0 ? 'PUT' : 'POST', $this->mapper->mapToStorage($model)
        );

        return $this->mapper->mapFromStorage($response);
    }
1179656
public function created(Model $model)
    {
        $langs = app('languages')->langs();
        DB::beginTransaction();
        try {
            foreach ($langs as $lang) {
                $this->saveTranslatedMessage($model, $lang);
            }
        } catch (Exception $ex) {
            DB::rollback();
            throw $ex;
        }
        DB::commit();
        return true;
    }
365310
public function deleteDirectories($model, $parentRelation = null)
    {
        $id = is_null($parentRelation) ? $model->id : $model->$parentRelation->id;
        foreach($this->options as $option){
            $this->deleteDirectory($option['path'] . "/{$id}");
        }
    }
715152
public function setModel(\Illuminate\Database\Eloquent\Model $model) {
        $this->model = $model;
        if ($this->model instanceof ResourceInterface) {
            $this->enableAutoEagerLoading = true;
        }
    }
499786
public function deleteByParent( $parentId, $parentType ) {

		//$modelClass	= static::$modelClass;

		//return $modelClass::deleteByParent( $parentId, $parentType );

		$models	= $this->getByParent( $parentId, $parentType );

		foreach ( $models as $model ) {

			$this->delete( $model );
		}
	}
929226
protected function getJoinClause(Model $parent, Relation $relation, $table, $type)
    {
        list($fk, $pk) = $this->getJoinKeys($relation);

        $join = (new Join($this->query, $type, $table))->on($fk, '=', $pk);

        if (in_array(SoftDeletes::class, class_uses_recursive($relation->getRelated()))) {
            $join->whereNull($relation->getRelated()->getQualifiedDeletedAtColumn());
        }

        if ($relation instanceof MorphOneOrMany) {
            $join->where($relation->getQualifiedMorphType(), '=', $parent->getMorphClass());
        } elseif ($relation instanceof MorphToMany || $relation instanceof MorphByMany) {
            $join->where($relation->getMorphType(), '=', $parent->getMorphClass());
        }

        return $join;
    }
154887
public function setChild($parent, $child)
    {
        if (is_string($parent)) {
            $name   = $parent;
            $parent = $this->getItem($parent);
            if (is_null($parent)) {
                throw new Exception("Unknown parent:$name at setChild");
            }
        }
        if (is_string($child)) {
            $name  = $child;
            $child = $this->getItem($child);
            if (is_null($child)) {
                throw new Exception("Unknown child:$name at setChild");
            }
        }
        if (isset($this->children[$parent->name][$child->name])) {
            return false;
        }

        return $this->addChild($parent, $child);
    }
97428
public void setParent(Model parent) {
        if (parent == this) {
            throw new IllegalArgumentException("The model can't be its own parent");
        }
        if (parent == null) {
            this.parent.children.remove(this);
        } else {
            parent.children.add(this);
        }
        this.parent = parent;
    }
919865
public function setModel(Model $model)
    {
        if (strpos($this->originalName, '.') !== false) {
            $parts = explode('.', $this->originalName);
            $name = array_pop($parts);

            while ($parts) {
                $part = array_shift($parts);
                $relation = $model->getAttribute($part);

                if ($relation instanceof Model) {
                    $model = $relation;
                    $this->setName($name);
                }
            }
        }

        return parent::setModel($model);
    }
1085425
public function invoke($node, NodeInterface $parent): void
    {
        if (! $node instanceof Node\Stmt\Use_ || ! $parent instanceof PhpFileModelInterface) {
            throw new Exception('UseNodeParser is made to parse a use node');
        }

        if ($node->type === Node\Stmt\Use_::TYPE_NORMAL) {
            foreach ($node->uses as $use) {
                $parent->addUse($use->getAlias()->name, $use->name->toString());
            }
        }
    }
724174
public function setModel(\Parable\ORM\Model $model)
    {
        $this->model = $model->reset();
        return $this;
    }
551611
public void init(Object parent, TableModel model)
    {
        super.init(parent, model);
        this.setModel(model);
    }
724700
public function afterLoad($model)
    {
        // we need to collect ID
        $this->id = $model->data[$this->master_field];
        if (!$this->id) {
            return;
        }

        try {
            $data = $model->persistence->load($model, $this->id, $this->foreign_table);
        } catch (Exception $e) {
            throw new Exception([
                'Unable to load joined record',
                'table' => $this->foreign_table,
                'id'    => $this->id,
            ], $e->getCode(), $e);
        }
        $model->data = array_merge($data, $model->data);
    }
428993
protected function initModelManager()
    {
        $managerClass = $this->getModel();
        $modelManager = $this->newModelManagerInstance($this->getModel());
        if ($modelManager instanceof RecordManager) {
            $this->modelManager = $this->newModelManagerInstance($this->getModel());
        } else {
            throw new Exception(
                "invalid ModelManager name [$managerClass] 
                for controller [" . $this->getClassName() . "]"
            );
        }
    }
73589
public function remove(Model $model = null)
    {
        if (func_num_args() === 0) {
            $this->connection->remove($this);
        } else {
            // avoid remove empty model
            if (is_null($model)) {
                throw new \Exception('[Norm/Collection] Cannot remove null model');
            }

            $this->applyHook('removing', $model);
            $result = $this->connection->remove($this, $model);

            if ($result) {
                $model->reset();
            }

            $this->applyHook('removed', $model);
        }
    }
14676
private function searchParentOfInHierarchical(ModelInterface $model)
    {
        if ($this->isRootModel($model)) {
            throw new DcGeneralInvalidArgumentException('Invalid condition, root models can not have parents!');
        }
        // Start from the root data provider and walk through the whole tree.
        // To speed up, some conditions have an inverse filter - we should use them!
        $config = $this->rootProvider->getEmptyConfig()->setFilter($this->rootCondition->getFilterArray());

        return $this->searchParentOfIn($model, $this->rootProvider->fetchAll($config));
    }