Attach a model instance to the parent model.

@param  \Globalis\PuppetSkilled\Database\Magic\Model  $model
@return \Globalis\PuppetSkilled\Database\Magic\Model
485273
public function save(Model $model)
    {
        $model->setAttribute($this->getForeignKeyName(), $this->getParentKey());

        return $model->save() ? $model : false;
    }
1345434
public function save(Model $model)
    {
        if ($this->fieldId) {
            $model->setAttribute($this->fieldKey, $this->fieldId);
        }

        return parent::save($model);
    }
408558
public function unlink(Model $model) {
        $this->getRelation($this->getAlias(get_class($model)))->unlink($model);

        return $this;
    }
1193229
public function setModel(Model $model)
    {
        $this->model = $model;
        $append = $this->getAppends();

        if ($append instanceof WithModelInterface) {
            $append->setModel($model);
        }

        return $this;
    }
224381
protected function getReference(Model $model)
    {
        $reference = $this->getReferenceValue($model);

        return $this->wrapWithLink($reference, $this->getLinkForReferenceModel($model));
    }
134431
public function setPrimaryModel(Model $model) {
        $this->_model = $model;
        $this->setPrimaryClass(get_class($model));

        return $this;
    }
528341
public function setModel($model)
    {
        $model->setSerializeNull($this->isSerializeNull());
        $this->setJson($model->toJson());

        return $this;
    }
809273
public function setModel(Model $model, $capsule)
    {
        $this->_model = $model;
        $this->_capsule = $capsule;

        return $this;
    }
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);
    }
150410
protected function setModelForElements(Model $model)
    {
        $this->getElements()->each(function ($element) use ($model) {
            $element = $this->getElementContainer($element);

            if ($element instanceof WithModelInterface) {
                $element->setModel($model);
            }
        });

        return $this;
    }
235540
public function moveAfter($model)
    {
        if ( ! $model ) {
            $this->moveFirst();
            return;
        }
        if ( is_numeric($model) ) {
            $model = call_user_func("$this->ownerClassName::findOne", $model);
        }
        $parentId = $this->parentIdField ? $model->{$this->parentIdField} : 0;
        if ( $model->{$this->sortField} == $this->defaultSortVal ) {
            $this->reorder($parentId);
            $model->refresh();
        }
        $nextIdx = $this->getMinIdx($parentId, $model->{$this->sortField});
        if ( $nextIdx - $model->{$this->sortField} < 2 ) {
            $this->reorder($parentId);
            $model->refresh();
            $nextIdx = $this->getMinIdx($parentId, $model->{$this->sortField});
        }
        $newIdx = $model->{$this->sortField} + floor(($nextIdx - $model->{$this->sortField})/2);
        if ( $this->parentIdField ) {
            call_user_func("$this->ownerClassName::updateAll", [
                $this->sortField => $newIdx,
                $this->parentIdField => $parentId,
            ], [ $this->primaryKey => $this->owner->primaryKey ]);
        } else {
            call_user_func("$this->ownerClassName::updateAll",
                [ $this->sortField => $newIdx ],
                [ $this->primaryKey => $this->owner->primaryKey ]);
        }
        $this->invalidateTagDependency();
    }
696965
protected function cloneModel($model) {
		$exempt = method_exists($model, 'getCloneExemptAttributes') ?
			$model->getCloneExemptAttributes() : null;
		$clone = $model->replicate($exempt);
		if ($this->write_connection) $clone->setConnection($this->write_connection);
		return $clone;
	}
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();
    }
845824
public function associate($model)
    {
        $this->parent->setAttribute(
            $this->foreignKey, $model instanceof Model ? $model->getKey() : null
        );

        $this->parent->setAttribute(
            $this->morphType, $model instanceof Model ? $model->getMorphClass() : null
        );

        return $this->parent->setRelation($this->relationName, $model);
    }
1086565
public function setModel($model)
    {
        // in case of Agile Data model - don't add it to object.
        // Agile Data Model owner is Agile Data Persistance and so be it.
        if ($model instanceof \atk4\data\Model) {
            return $this->model = $model;
        }

        $model = $this->app->normalizeClassName($model, 'Model');
        $this->model = $this->add($model);

        return $this->model;
    }
974888
public function prepare(Model $model)
    {
        if (empty($this->primaryKey)) {
            if (!empty($this->parents)) {
                $field = $this->parents;
                $field->primaryKey = true;
                $this->setupPrimaryKey($field);
                if (!$field->relation->parentLink) {
                    throw new ImproperlyConfigured(
                        sprintf('Add parentLink=True to %s.', $field)
                    );
                }
            } else {
                $field = AutoField::createObject(
                    [
                        'verboseName' => 'ID',
                        'primaryKey' => true,
                        'autoCreated' => true,
                    ]
                );

                $model->addToClass('id', $field);
            }
        }
    }
857291
public function create(Model $model)
    {
        $metadata = $model->getMetadata();
        $insert = $this->createInsertObj($model);
        $this->getQuery()->executeInsert($metadata, $insert);
        return $model;
    }