/** * Insert model in database * @param \Sebk\SmallOrmBundle\Dao\Model $model * @return \Sebk\SmallOrmBundle\Dao\AbstractDao */ protected function insert(Model $model) { $sql = "INSERT INTO " . $this->connection->getDatabase() . "." . $this->dbTableName . " "; $fields = $model->toArray(false, true); $columns = array(); foreach ($fields as $key => $val) { $queryFields[$key] = ":{$key}"; $columns[] = $this->getField($key)->getDbName(); } $sql .= "(" . implode(", ", $columns) . ")"; $sql .= " VALUES("; $sql .= implode(", ", $queryFields); $sql .= ");"; $this->connection->execute($sql, $fields); if ($this->connection->lastInsertId() !== null) { foreach ($model->getPrimaryKeys() as $key => $value) { if ($value === null) { $method = "set" . $key; $model->{$method}($this->connection->lastInsertId()); } } } $model->fromDb = true; $model->altered = false; return $this; }