Insert the record to the database

@return Wedeto\DB\Model the saved object
779821
public function insert($db = null)
    {
        $this->getDAO($this->getDB($db))->save($this);
        return $this;
    }
1026552
final public function create() {

		$app = Application::getInstance();
		$class = get_called_class();
		
		if (!$this->areKeysPopulated() and !$this->db->isAutoIncrement(static::TABLE_NAME)) {
			$app->logEvent('The object’s ' . implode(', ', $this->keyProperties) . ' properties must be populated in order to create a ' . $class . ' record');
			return FALSE;
		}
		
		// hook for tasks to be executed before creation
		$this->beforeCreate();
		
		// get list of class property names
		$props = array_keys(static::getBinds());
		
		// insert the object as db record
		$dbObj = $this->prepareData($props);
		$res = $this->db->insertObject(static::TABLE_NAME, $dbObj);

		// get last insert id if not compound key
		if (!static::hasCompoundKey()) {

			$lastInsertId = $this->db->getLastInsertId();
		
			$key = $this->keyProperties[0];
			
			if ('int' == $this->getPropertyType($key)) {
				$this->{$key} = (int)$lastInsertId;
			} else {
				$this->{$key} = $lastInsertId;
			}
			
		}

		// set logs
		$keyParts = array();
			
		foreach ($this->keyProperties as $prop) {
			$keyParts[] = $prop . '=' . $this->{$prop};
		}
		
		// log as application event
		$app->logEvent('Created a new ' . $class . ' object with ' . implode(', ' , $keyParts));
		
		// hook for tasks to be executed after creation
		$this->afterCreate();
		
		return (bool)$res;
	
	}
7061
public function save()
    {
    	if($this->exists())
    	{
			// update
			$query = new Query(static::table());
			$query->update($this->getData(), $this->getIdentifyCondition());
			return $query->execute();
    	}
    	else
    	{
    		// insert
			$query = new Query(static::table());
			$query->insert($this->getData());
            if($query->execute())
                $this->from_db = true;
            return $query->success();
    	}
    }
103035
public function save()
    {
        if (true === $this->modified) {
            if (null !== $this->uid) {
                $this->update($this->parameters());
            } else {
                $this->insert($this->parameters());
            }
        }

        return $this;
    }
166567
private function insert()
    {
        $insert = $this->dsql();

        // Performs the actual database changes. Throw exception if problem occurs
        foreach ($this->elements as $name => $f) {
            if ($f instanceof Field) {
                if (!$f->editable() && !$f->system()) {
                    continue;
                }
                if (!isset($this->dirty[$name]) && $f->defaultValue() === null) {
                    continue;
                }

                $f->updateInsertQuery($insert);
            }
        }
        $this->hook('beforeInsert', array(&$insert));
        //delayed is not supported by INNODB, but what's worse - it shows error.
        //if($this->_save_as===false)$insert->option_insert('delayed');
        $id = $insert->insert();
        if ($id == 0) {
            // no auto-increment column present
            $id = $this->get($this->id_field);

            if ($id === null && $this->_save_as !== false) {
                throw $this->exception('Please add auto-increment ID column to your table or specify ID manually');
            }
        }
        $res = $this->hook('afterInsert', array($id));
        if ($res === false) {
            return $this;
        }

        if ($this->_save_as === false) {
            return $this->unload();
        }
        if ($this->_save_as) {
            $this->unload();
        }
        $o = $this->_save_as ?: $this;

        if ($this->fast && !$this->_save_as) {
            $this[$this->id_field] = $this->id = $id;

            return $this;
        }
        $res = $o->tryLoad($id);
        if (!$res->loaded()) {
            throw $this->exception('Saved model did not match conditions. Save aborted.');
        }

        return $res;
    }
1354684
protected function _insert()
    {
        // set oxinsert
        $sNow = date('Y-m-d H:i:s', \OxidEsales\Eshop\Core\Registry::getUtilsDate()->getTime());
        $this->oxarticles__oxinsert = new \OxidEsales\Eshop\Core\Field($sNow);
        if (!is_object($this->oxarticles__oxsubclass) || $this->oxarticles__oxsubclass->value == '') {
            $this->oxarticles__oxsubclass = new \OxidEsales\Eshop\Core\Field('oxarticle');
        }

        return parent::_insert();
    }
329594
private function insert()
    {
        $db = new Db();

        //Pegando um ID válido
        $id = $db->query('SELECT MAX(id)id FROM article');
        $this->id = $id[0]->get('id')+1;

                //Insert into content table
        $row = $db->query('INSERT INTO articlecontent
                            SET article = :article,
                                editdate = :editdate,
                                content = :content',
                        [':article' => $this->id,
                         ':editdate' => $this->editdate,
                         ':content' => $this->content]);

        //check insert
        if (!$row) {
            //TODO: insert a ROWBACK if not inserted.

            return false;
        }
        
        //Insert into article table
        $row = $db->query('INSERT INTO article
                            SET id = :id,
                                category = :category,
                                author = :author,
                                pubdate = :pubdate,
                                editdate = :editdate,
                                link = :link,
                                tags = :tags,
                                status = :status,
                                title = :title,
                                media = :media,
                                resume = :resume ',
                        [':id'=>$this->id,
                         ':category' => $this->category,
                         ':author' => $this->author,
                         ':pubdate' => $this->pubdate,
                         ':editdate' => $this->editdate,
                         ':link' => $this->link,
                         ':tags' => $this->tags,
                         ':status' => $this->status,
                         ':title' => $this->title,
                         ':media' => $this->media,
                         ':resume' => $this->resume
                        ]);

        return $row;
    }
45933
public function save()
    {
        $data = $this->getData();
        if (!$this->currentId)
        {
            $config = $this->getConfig();
            $config->last_id++;
            $this->tableFields->{$this->databaseConfig->getIdentifier()} = $config->last_id;
            array_push($data,$this->tableFields);
            $this->databaseConfig->put($config);
        }
        else
        {
            $this->tableFields->{$this->databaseConfig->getIdentifier()} = $this->currentId;
            $data[$this->currentKey]    = $this->tableFields;
        }
        $this->databaseDocument->put($data);
        return (!$this->currentId) ? $this->setFields() : $this;
    }
971886
public function insert()
    {
        $this->SQLTemplate('insert')->execute();

        return
            $this->hasInsertOption('ignore') ? null :
            $this->owner->lastID();
    }
356670
public function Form()
    {
        if ($this->currentRecord) {
            $fields = $this->currentRecord->getFrontEndFields();
            $required = $this->currentRecord->getRequiredFields();
        } else {
            $fields = singleton($this->recordType)->getFrontEndFields();
            $required = singleton($this->recordType)->getFrontEndFields();
        }

        $fields->push(new HiddenField('ID'));
        $form = new Form($this, __function__, $fields, new FieldList(new FormAction('doSave', 'Save')), $required);
        if ($this->currentRecord) {
            $form->loadDataFrom($this->currentRecord);
        }
        return $form;
    }
1206445
public function commit()
    {
        try {
            $prepared = Agl::app()->getDb()->getConnection()->prepare("
                INSERT INTO
                    `" . $this->getDbPrefix() . $this->_dbContainer . "`
                (" . $this->_getFields() . ")
                VALUES
                (" . $this->_getPreparedFields() . ")
            ");

            if (! $prepared->execute($this->_fields)) {
                $error = $prepared->errorInfo();
                throw new Exception("The insert query failed (table '" . $this->getDbPrefix() . $this->_dbContainer . "') with message '" . $error[2] . "'");
            }

            if (Agl::app()->isDebugMode()) {
                Agl::app()->getDb()->incrementCounter();
            }

            $this->_setId(Agl::app()->getDb()->getConnection()->lastInsertId());

            return $prepared->rowCount();
        } catch (Exception $e) {
            throw new Exception($e);
        }

        return true;
    }
985887
public static function insert()
    {
        $result = null;

        switch (static::getDriver()) {
            case self::DRIVER_MYSQL:
                $result = new \pwf\components\querybuilder\adapters\MySQL\InsertBuilder();
                break;
            case self::DRIVER_PG:
                $result = new \pwf\components\querybuilder\adapters\PostgreSQL\InsertBuilder();
                break;
            default:
                throw new \Exception('Wrong query builder driver');
        }

        return $result;
    }
1155214
protected function insert()
    {
        $aggregate = $this->aggregate;

        $attributes = $aggregate->getRawAttributes();

        $keyName = $aggregate->getEntityMap()->getKeyName();

        // Check if the primary key is defined in the attributes
        if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) {
            $this->query->insert($attributes);
        } else {
            // Prevent inserting with a null ID
            if (array_key_exists($keyName, $attributes)) {
                unset($attributes[$keyName]);
            }

            if (isset($attributes['attributes'])) {
                unset($attributes['attributes']);
            }

            $id = $this->query->insertGetId($attributes, $keyName);

            $aggregate->setEntityAttribute($keyName, $id);
        }
    }
1164668
protected function _insert()
    {
        $myConfig = \OxidEsales\Eshop\Core\Registry::getConfig();
        $oUtilsDate = \OxidEsales\Eshop\Core\Registry::getUtilsDate();

        //V #M525 orderdate must be the same as it was
        if (!$this->oxorder__oxorderdate || !$this->oxorder__oxorderdate->value) {
            $this->oxorder__oxorderdate = new \OxidEsales\Eshop\Core\Field(date('Y-m-d H:i:s', $oUtilsDate->getTime()), \OxidEsales\Eshop\Core\Field::T_RAW);
        } else {
            $this->oxorder__oxorderdate = new \OxidEsales\Eshop\Core\Field(
                $oUtilsDate->formatDBDate(
                    $this->oxorder__oxorderdate ? $this->oxorder__oxorderdate->value : null,
                    true
                )
            );
        }

        $this->oxorder__oxshopid = new \OxidEsales\Eshop\Core\Field($myConfig->getShopId(), \OxidEsales\Eshop\Core\Field::T_RAW);
        $this->oxorder__oxsenddate = new \OxidEsales\Eshop\Core\Field(
            $oUtilsDate->formatDBDate(
                $this->oxorder__oxsenddate ? $this->oxorder__oxsenddate->value : null,
                true
            )
        );

        $blInsert = parent::_insert();

        return $blInsert;
    }
768037
public function insert()
    {
        Observer::dispatch(Observer::EVENT_ITEM_INSERT_BEFORE, array(
            'item' => $this
        ));

        $this->{static::DATEADDFIELD} = DateData::now();
        $insert = new Insert($this->_dbContainer);
        $insert->addFields($this->_fields);
        $insert->commit();
        $this->setId($insert->getId());
        $this->_origFields = $this->_fields;

        Observer::dispatch(Observer::EVENT_ITEM_INSERT_AFTER, array(
            'item' => $this
        ));

        return $this;
    }
833815
public function add()
    {
        $this->view = 'scaffold/add';
        $form = new Form(
            "add-{$this->get('modelSingular')}", $this->getDescriptor()
        );
        if ($this->request->isPost()) {
            $form->setData($this->request->getPost());
            if ($form->isValid()) {
                try {
                    $modelClass = $this->getModelName();
                    /** @var Model $model */
                    $model = new $modelClass($form->getValues());
                    $model->save();
                    $name = ucfirst($this->get('modelSingular'));
                    $this->addSuccessMessage(
                        "{$name} successfully created."
                    );
                    $pmk = $model->getPrimaryKey();
                    $this->redirect(
                        $this->_basePath .'/'.
                        $this->get('modelPlural').'/show/'.$model->$pmk
                    );
                    return;
                } catch (Database\Exception $exp) {
                    $this->addErrorMessage(
                        "Error while saving {$this->get('modelSingular')}} " .
                        "data: {$exp->getMessage()}"
                    );
                }
            } else {
                $this->addErrorMessage(
                    "Cannot save {$this->get('modelSingular')}. " .
                    "Please correct the errors bellow."
                );
            }
        }
        $descriptor =  $this->getDescriptor();
        $this->set(compact('form', 'descriptor'));
    }
273981
private function getInsertQuery()
    {
        return 'INSERT INTO '.$this->parts['from'][0]['table'].
               ' ('.implode(', ', array_keys($this->parts['set'])).')'.
               ' VALUES'.
               ' ('.implode(', ', $this->parts['set']).')';
    }