/** * Insert new data * @param array $data * @return Row */ public function create(array $data = null) { $assign = true; if (null === $data) { $data = $this->data; $assign = false; } $data = $this->intersect($data); if ($this->table->autoIncrement() && ($pk = $this->table->pk())) { if (isset($data[$pk])) { unset($data[$pk]); } } $this->data = $data; if ($duplicate = $this->isDuplicate(true)) { throw new onDuplicateRow($duplicate); } $this->table->triggerEvent('before.create', $this); $builder = $this->table->builder(); $query = $builder->insert($this->data); $this->table->connection()->query($query); if ($this->table->autoIncrement() && ($pk = $this->table->pk())) { $this->data[$pk] = $this->table->connection()->last_insert_id(); } $this->table->triggerEvent('after.create', $this); if ($assign) { $data =& $this; } return $this; }