Insert a model
@param string $model the XML data
@return boolean true if inserted
542156
public function insertModel($namespace, $model){

		$returnValue = false;
		if(!preg_match("/#$/", $namespace)){
			$namespace .= '#';
		}

		
        $modFactory = new core_kernel_api_ModelFactory();
        $returnValue = $modFactory->createModel($namespace, $model);
               

        return $returnValue;
	}
1107932
public function beforeInsert($model, &$data)
    {
        if ($this->weak) {
            return;
        }

        // The value for the master_field is set, so we are going to use existing record anyway
        if ($model->hasField($this->master_field) && $model[$this->master_field]) {
            return;
        }

        $insert = $this->dsql();
        $insert->mode('insert');
        $insert->set($model->persistence->typecastSaveRow($model, $this->save_buffer));
        $this->save_buffer = [];
        $insert->set($this->foreign_field, null);
        $insert->insert();
        $this->id = $this->owner->lastInsertID();

        if ($this->join) {
            $this->join->set($this->master_field, $this->id);
        } else {
            $data[$this->master_field] = $this->id;
        }
    }
982479
public function dynamic($model, &$attributes = null)
    {
        // dynamic migration is not allowed when attributes are locked explicitly
        $table_schema = Cache::get($model->getTable());

        if (isset($table_schema['lock_attributes']) && $table_schema['lock_attributes']) {
            return true;
        }

        // Cached attributes
        $cached_attributes = array_map(function($item) { return $item['name']; }, isset($table_schema['attributes']) ? $table_schema['attributes'] : array());

        $config = array(
            'lock_attributes' => false,
            'attributes' => array()
        );

        foreach ($attributes as $field => $value) {
            // extract datatype from field value to migrate
            $datatype = strtolower(gettype($value));

            // ignore 'null' data-types
            if ($datatype == 'null') {
                unset($attributes[$field]);
                continue;
            }

            // Detect large text blocks to declare 'text' datatype.
            if ($datatype == 'string' && strlen($value) > 255) {
                $datatype = 'text';
            }

            if ($datatype !== 'array' && !in_array($field, $cached_attributes)) {
                $config['attributes'][] = array('name' => $field, 'type' => $datatype);
            }
        }

        if (count($config['attributes']) > 0) {
            return $this->migrate($model, $config, true);
        } else {
            return false;
        }
    }
530209
public function push(Model $model): bool
    {
        if ($this->has($model)) {
            return $this->get($model);
        }

        return (bool) $this->model->collections()->save(
            new Collection([
                'item_id'         => $model->id,
                'item_type'       => get_class($model),
                'collection_name' => $this->collectionName,
            ])
        );
    }
1296306
public function associate($model)
    {
        $foreignKey = $this->foreignKey;
        $pk         = $model->getPk();

        $this->parent->setAttr($foreignKey, $model->$pk);
        $this->parent->save();

        return $this->parent->setRelation($this->relation, $model);
    }
622645
public function insertModels(Models $models)
    {
        foreach ($models as $model) {
            $this->dispatchBeforeEvent($model, Event::INSERT);
            $this->dispatchBeforeEvent($model, Event::SAVE);
        }

        $this->insertAll()->executeModels($models);

        foreach ($models as $model) {
            $model
                ->resetOriginals()
                ->setState(State::SAVED);

            $this->dispatchAfterEvent($model, Event::INSERT);
            $this->dispatchAfterEvent($model, Event::SAVE);
        }

        return $this;
    }
1061806
public function create(Model $model) {

		return $this->_populate(
			$this->_request('post', $this->_getUrl('collection'), [
				'json' => $model->getAttributes()
			]),
			false
		);
	}
1050242
public function persist(AbstractModel $model)
    {
        $collection = $this->getCollectionBy(
            $this->getModelConfiguration(get_class($model))
        );

        $this->setLastResult(
            $collection->insert($this->getModelDataAsArray($model))
        );

        return (bool) ($this->lastResult) ? $this->lastResult[self::RESULT_OK_INDEX] : false;
    }
738319
private function create($model)
    {
        $attrs = $this->getMutableAttributes($model);
        $this->db
            ->connect()
            ->insert($this->table, array_keys($attrs))
            ->execute(array_values($attrs));
        $model->{$this->key} = $this->db->lastInsertId();
    }
34990
public function insert($model, array $options = [])
    {
        $values = $this->convertValues->convertToMongoValues(get_values($model), []);

        $result = $this->collection->insertOne($values, $options);
        if (false == $result->isAcknowledged()) {
            throw new \LogicException('Operation is not acknowledged');
        }

        set_object_id($model, new ObjectID((string) $result->getInsertedId()));
        $this->changesCollector->register($model, $values);

        return $result;
    }
1318157
public function submit($model, $values)
    {
        $exists = $this->exists($model, $values);

        if (!$exists) {
            $exists = $this->insert($model, $values);
        }

        return $exists;
    }
134562
public function setModel($model)
    {
        // convert the model class into an instance
        if (!is_object($model)) {
            $this->modelClass = $model;
            $model = new $model($this->modelId);
        } else {
            $this->modelId = $model->ids();
            $this->modelClass = get_class($model);
        }

        $this->model = $model;

        return $this;
    }
825210
public function beforeSave(Model &$model)
    {

        if ($model->isNew()) {
            if (empty($model->parent)) {
                $this->insertModelAsLastRoot($model);
            } else {
                $this->insertModelAsLastChildOf($model, $model->parent);
            }
        } else {
            /** @var \T4\Orm\Model $class */
            $class = get_class($model);
            $oldParent = empty($model->__prt) ? null : $class::findByPk($model->__prt);
            if ($oldParent != $model->parent) {
                $model->refreshTreeColumns();
                if (empty($model->parent)) {
                    $this->insertModelAsLastRoot($model);
                } else {
                    /** @var \T4\Dbal\Connection $connection */
                    $connection = $class::getDbConnection();
                    $query = new Query();
                    $query
                        ->select('count(*)')
                        ->from($class::getTableName())
                        ->where('__id=:id AND __lft>:lft AND __rgt<:rgt')
                        ->params([':id' => $model->parent->getPk(), ':lft'=>$model->__lft, ':rgt'=>$model->__rgt]);

                    if ($connection->query($query)->fetchScalar() > 0) {
                        throw new Exception('Parent must not be in children!');
                    }

                    $this->insertModelAsLastChildOf($model, $model->parent);
                }
            }
        }

        return true;

    }
261262
protected function newModelQuery($model = null)
    {
        return is_null($model)
                ? $this->createModel()->newQuery()
                : $model->newQuery();
    }
1145355
public function persist(AbstractModel $model, $forceInsert = false)
    {
        $retval = false;
        try {
            list ($query, $param, $type) = $this->getPersistData($model, $forceInsert);
            $stmt = $this->prepare($query, $param);

            if ($stmt->execute()) {

                if ($type == MySQLQueryBuilder::INSERT_QUERY) {
                    $this->setModelPrimaryKey($this->db->getLastInsertId(), $model);
                }

                $retval = true;
            } else {
                $this->log(ILog::T_ERROR, print_r($stmt->errorInfo()), true);
                $retval = false;
            }
        } catch (\Exception $e) {
            $this->log(ILog::T_ERROR, $e->getMessage(), true);
            return false;
        }

        // add to the modelCache
        $this->modelCache->set($model);

        return $retval;
    }
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;
    }
724716
public function save(Model $model)
    {
        $model->setAttribute($this->getPlainMorphType(), $this->morphClass);

        return parent::save($model);
    }