Add a model, traverse all the linked models recursively and add them too.

@param  AbstractModel $model
@return Save
766721
public function add(AbstractModel $model)
    {
        if (! $this->has($model)) {
            $this->addShallow($model);

            $modelLinks = $model->getRepo()->getLinkMap()->get($model);

            foreach ($modelLinks->getModels() as $linkedModel) {
                $this->add($linkedModel);
            }
        }

        return $this;
    }
1039290
public function persist(ModelInterface $model): RepositoryInterface
    {
        $id = $model->getId();
        $row = $model->toPersistence();

        $this->connection->beginTransaction();

        $alreadyExists = (bool) $this->find($id);

        $stack = new RelatedModelManipulationStack();

        foreach ($row as $field => $value) {
            if ($value instanceof ModelCollectionInterface) {
                $stack->addToRemoveModels($value->getInitialModels());
                $stack->addToPersistModels($value->getModels());
                unset($row[$field]);
            } elseif ($value instanceof ModelReferenceInterface) {
                $row[$field.'Id'] = $this->persistModelReference($value, $stack);

                unset($row[$field]);
            }
        }

        if (!$alreadyExists) {
            $this->insert($id, $row);
        } else {
            $this->update($id, $row);
        }

        $this->removeRelatedModels($stack->getToRemoveModels());
        $this->persistRelatedModels($stack->getToPersistModels());

        $this->storageCache->set($id, $row);

        $this->connection->commit();

        return $this;
    }
142672
protected function save($model)
    {
        $method = $this->methods['save'];

        if (!method_exists($model, $method) || !is_callable([$model, $method])) {
            throw new SaveMethodNotFoundException(get_class($model), $method);
        }

        return $model->$method();
    }
409185
public function save(Model $model)
    {
        $foreignKey = $this->getForeignKeyName();
        $parentKeyValue = $this->getParentKey();

        if (is_array($foreignKey)) { //Check for multi-columns relationship
            foreach ($foreignKey as $index => $key) {
                $model->setAttribute($key, $parentKeyValue[$index]);
            }
        } else {
            $model->setAttribute($foreignKey, $parentKeyValue);
        }

        return $model->save() ? $model : false;
    }
1024470
public function push(AbstractModel $model)
    {
        $this->validateAdd($model);
        if (true === $this->willAdd($model)) {
            return $this;
        }
        if (true === $this->willRemove($model)) {
            $this->evict('removed', $model);
            $this->set('models', $model);
            return $this;
        }
        if (true === $this->hasOriginal($model)) {
            return $this;
        }
        $this->set('added', $model);
        $this->set('models', $model);
        return $this;
    }
1075635
public function addModel(ModelInterface $model): ModelCollectionInterface
    {
        $this->propertyReflection->setValue($model, $this->foreignId);

        $this->models[$model->getId()] = $model;

        return $this;
    }
272752
public function saveToModel(Model $model, Request $request)
    {
        $this->setModel($model);

        $model->setAttribute($this->getFieldName(), $this->getSaveValue($request));

        return $this;
    }
941371
protected function saveHasOneRelations(Model $model)
    {
        foreach ($model->getRelations() as $name => $relation) {
            if ($model->{$name}() instanceof HasOneOrMany && ! is_null($relation)) {
                if (is_array($relation) || $relation instanceof \Traversable) {
                    $model->{$name}()->saveMany($relation);
                } else {
                    $model->{$name}()->save($relation);
                }
            }
        }
    }
410513
def _attach_to_model(self, model):
        
        super(RelatedFieldMixin, self)._attach_to_model(model)

        if model.abstract:
            # do not manage the relation if it's an abstract model
            return

        # now, check related_name and save the relation in the database

        # get related parameters to identify the relation
        self.related_name = self._get_related_name()
        self.related_to = self._get_related_model_name()

        # create entry for the model in the _relations list of the database
        if not hasattr(self.database, '_relations'):
            self.database._relations = {}
        self.database._relations.setdefault(self.related_to, [])

        # check unicity of related name for related model
        self._assert_relation_does_not_exists()

        # the relation didn't exists, we can save it
        relation = (self._model._name, self.name, self.related_name)
        self.database._relations[self.related_to].append(relation)
134129
protected function save($model)
    {
        $method = $this->methods['save'];

        if (!method_exists($this->storage, $method) || !is_callable([$this->storage, $method])) {
            throw new SaveMethodNotFoundException(get_class($this->storage), $method);
        }

        $this->storage->$method($model);
        $this->flush();

        return true;
    }
986348
public function willAdd(AbstractModel $model)
    {
        $key = $model->getCompositeKey();
        return isset($this->added[$key]);
    }
1081700
public function setModel($model)
	{
		$this->model = $model;
		
		$this->from($this->model->metableTable() . ' AS m');
		
		if ($this->model->metableTableSoftDeletes()) {
			$this->whereNull('m.' . $this->model->getDeletedAtColumn());
		}
		
		return $this;
	}
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);
    }
1265567
protected function insertModelAsFirstRoot(Model &$model)
    {
        /** @var \T4\Orm\Model $class */
        $class = get_class($model);
        $tableName = $class::getTableName();
        /** @var \T4\Dbal\Connection $connection */
        $connection = $class::getDbConnection();

        $query = (new Query())
            ->select('MIN(__lft)')->from($tableName);
        $minLft = (int)$connection->query($query)->fetchScalar();

        $width = $model->getTreeWidth();
        $this->expandTreeBeforeLft($connection, $tableName, $minLft, $width);

        if (!$model->isNew()) {
            $model->refreshTreeColumns();
            $query = (new Query("
                UPDATE `" . $tableName . "`
                SET
                    `__lft` = `__lft` - :lft + :min,
                    `__rgt` = `__rgt` - :lft + :min,
                    `__lvl` = `__lvl` - :lvl
                WHERE `__lft` >= :lft AND `__rgt` <= :rgt
                "))
                ->params([':min' => $minLft, ':width'=> $width, ':lft' => $model->__lft, ':rgt' => $model->__rgt, ':lvl' => $model->__lvl]);
            $connection->execute($query);
            $this->removeFromTreeByElement($model);
        }

        $model->__lft = $minLft;
        $model->__rgt = $model->__lft + $width;
        $model->__lvl = 0;
        $model->__prt = 0;
    }
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);
    }
1223383
public function has(AbstractModel $model)
    {
        $key = $model->getCompositeKey();
        return isset($this->models[$key]);
    }
1345434
public function save(Model $model)
    {
        if ($this->fieldId) {
            $model->setAttribute($this->fieldKey, $this->fieldId);
        }

        return parent::save($model);
    }