Set hasAndBelongsToMany data for forms. This allows for saving of associated data.

@param Model $model
439569
public function setHabtmData(Model $model) {
        foreach ($model->hasAndBelongsToMany as $assoc) {
            if (!$assoc['showInForm']) {
                continue;
            }

            $object = Admin::introspectModel($assoc['className']);
            $variable = Inflector::variable(Inflector::pluralize(preg_replace('/(?:_id)$/', '', $assoc['associationForeignKey'])));

            $this->Controller->set($variable, $this->getRecordList($object));
        }
    }
802229
protected function updateBlameables(Model $model)
    {
        $user = $this->getActiveUser();

        if (is_null($user)) {
            return; // Todo: might be wise to implement loggin here
        }

        // Set updated-by if it has not been touched on this model
        if ($this->isBlameable($model, 'updated') && !$model->isDirty($this->getColumn($model, 'updated'))) {
            $this->setUpdatedBy($model, $user);
        }

        // Determine if we need to touch the created stamp
        if ($model->exists) {
            return;
        }

        // Set created-by if the model does not exist
        if ($this->isBlameable($model, 'created') && !$model->isDirty($this->getColumn($model, 'created'))) {
            $this->setCreatedBy($model, $user);
        }
    }
188783
public function save(Model $model)
    {
        $this->setForeignAttributesForCreate($model);

        return $model->save() ? $model : false;
    }
134431
public function setPrimaryModel(Model $model) {
        $this->_model = $model;
        $this->setPrimaryClass(get_class($model));

        return $this;
    }
564347
public function setAssociationCounts(Model $model) {
        $counts = array();

        foreach (array($model->hasMany, $model->hasAndBelongsToMany) as $property) {
            foreach ($property as $alias => $assoc) {
                $class = isset($assoc['with']) ? $assoc['with'] : $assoc['className'];

                $counts[$alias] = Admin::introspectModel($class)->find('count', array(
                    'conditions' => array($assoc['foreignKey'] => $model->id),
                    'contain' => false
                ));
            }
        }

        $this->Controller->set('counts', $counts);
    }
10165
protected function setForeignAttributesForCreate(Model $model)
	{
		$model->{$this->getPlainForeignKey()} = $this->getParentKey();

		$model->{last(explode('.', $this->morphType))} = $this->morphClass;
	}
922982
public function setModel($model)
    {
        $this->model = get_class($model);
        $this->modelId = $model->id;
        
        return $this;
    }
1146008
public function setExtraModelValues(array $fields, &$model)
    {
        //make sure the relationships are loaded
        foreach ($fields as $name => $field) {
            if ($field->getOption('relationship')) {
                $this->setModelRelationship($model, $field);
            }

            //if this is a setter field, unset it
            if ($field->getOption('setter')) {
                $model->__unset($name);
            }
        }
    }
1042697
public function updateDeleteBlameable(Model $model)
    {
        $user = $this->getActiveUser();

        if (is_null($user)) {
            return;
        }

        // Set deleted-at if it has not been touched
        if ($this->isBlameable($model, 'deleted') && !$model->isDirty($this->getColumn($model, 'deleted'))) {
            $this->setDeletedBy($model, $user);
            $model->save();
        }

    }
1280172
public function setModel($model, $ids = [])
    {
        $this->model = $model;
        $this->ids = Arr::wrap($ids);

        $this->message = "No query results for model [{$model}]";

        if (count($this->ids) > 0) {
            $this->message .= ' '.implode(', ', $this->ids);
        } else {
            $this->message .= '.';
        }

        return $this;
    }
33253
public function setModel(ModelInterface $model)
    {
        $this->model = $model;
        $this->configureWorkflow($this->model->getWorkflowName());
    }
691738
public function deleting($model)
    {
        if ($model->accountableEnabled() &&
            collect(class_uses($model))->contains(SoftDeletes::class)) {
            $model->{$this->config['column_names']['deleted_by']} = $this->accountableUserId();

            $model->save();
        }
    }
229766
protected function setModelAttributes($model)
    {
        $this->setModelClass(Inflector::getClassNameFromNamespace($model));

        if (!property_exists($model, 'tableName') || is_null($this->tableName)) {
            $this->setTableName(Inflector::tabilize($this->getModelClass()));
        }

        if (!property_exists($model, 'database') || is_null($this->database)) {
            $this->setDatabase($this->queryBuilder()->getDefaultConnection());
        } else {
            $this->setDatabase($this->database);
        }

        if (is_null($this->getDatabase())) {
            throw new \InvalidArgumentException(
                'Please specify database name in your model. '.get_called_class()
            );
        }

        $this->setPrimarykey();
    }
1083491
public function saved(Model $model)
    {
        if ($model::$__es_enable && $model->shouldIndex())
        {
            Queue::push('Iverberk\Larasearch\Jobs\ReindexJob', $this->findAffectedModels($model));
        }
    }
535650
public function save(Model $model)
    {
        $model->setAttribute($this->getPlainForeignKey(), $this->getParentKey());

        return $model->save() ? $model : false;
    }
789625
public function saveEntity(Model $model)
    {
        $cacheKey = $this->cacheKey($model->toArray());
        $this->cache->forget($cacheKey);
        return $model->save();
    }
52687
protected function saveColumns(Model $model)
    {
        /** @var \T4\Orm\Model $class */
        $class = get_class($model);
        $columns = $class::getColumns();
        $relations = $class::getRelations();
        $cols = [];
        $prep = [];
        $sets = [];
        $data = [];

        foreach ($columns as $column => $def) {
            if (isset($model->{$column}) && null === $model->{$column} && isset($def['default'])) {
                $cols[] = $this->quoteName($column);
                $prep[] = ':' . $column;
                $sets[] = '' . $this->quoteName($column) . '=:' . $column;
                $data[':' . $column] = $def['default'];
            } else {
                $cols[] = $this->quoteName($column);
                $prep[] = ':' . $column;
                $sets[] = '' . $this->quoteName($column) . '=:' . $column;
                switch ($def['type']) {
                    case 'boolean':
                        $data[':' . $column] = (int)$model->{$column};
                        break;
                    default:
                        $data[':' . $column] = $model->{$column};
                        break;
                }
            }
        }

        foreach ($relations as $rel => $def) {
            switch ($def['type']) {
                case $class::BELONGS_TO:
                    $column = $class::getRelationLinkName($def);
                    if (!in_array($column, $cols)) {
                        // @todo: test this!
                        // if (isset($model->{$column}) && !is_null($model->{$column})) {
                        if (isset($model->{$column})) {
                            $cols[] = $this->quoteName($column);
                            $prep[] = ':' . $column;
                            $sets[] = '' . $this->quoteName($column) . '=:' . $column;
                            $data[':' . $column] = $model->{$column};
                        } elseif (isset($model->{$rel}) && $model->{$rel} instanceof Model) {
                            $cols[] = $this->quoteName($column);
                            $prep[] = ':' . $column;
                            $sets[] = '' . $this->quoteName($column) . '=:' . $column;
                            $data[':' . $column] = $model->{$rel}->getPk();
                        }
                    }
                    break;
            }
        }

        $connection = $class::getDbConnection();
        if ($model->isNew()) {
            $sql = '
                INSERT INTO ' . $this->quoteName($class::getTableName()) . '
                (' . implode(', ', array_unique($cols)) . ')
                VALUES
                (' . implode(', ', array_unique($prep)) . ')
                RETURNING ' . $class::PK . '
            ';
            $res = $connection->query($sql, $data);
            $model->{$class::PK} = $res->fetch()[$class::PK];
        } else {
            $sql = '
                UPDATE ' . $this->quoteName($class::getTableName()) . '
                SET ' . implode(', ', array_unique($sets)) . '
                WHERE "' . $class::PK . '"=\'' . $model->{$class::PK} . '\'
            ';
            $connection->execute($sql, $data);
        }

        return $model;

    }