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;
}