public function insert(Dbi_Record $record) { self::$queryCount++; $data = $record->getArray(!$this->enforceSchemas); // Get rid of fields that are not defined in the schema. // TODO: Should undefined fields generate an error? foreach ($data as $key => $value) { if (is_null($record->model()->field($key))) { unset($data[$key]); } else { // Convert arrays to JSON // (Objects depend on __toString() for conversion) if (is_array($value)) { $data[$key] = json_encode($value); } } } $insert = new BuildSql_Insert($record->model()->prefix() . $record->model()->name(), $data); mysql_query($insert->query()); if (mysql_error()) { throw new Exception(mysql_error()); } $primary = $record->model()->index('primary'); if (is_array($primary) && count($primary['fields']) == 1) { $data[$primary['fields'][0]] = mysql_insert_id(); } // Return the data that was saved so Dbi_Record objects can update // automatically generated primary keys return $data; }
public function insert(Dbi_Record $record) { $data = $record->getArray(!$this->enforceSchemas); // Get rid of fields that are not defined in the schema. // TODO: Should undefined fields generate an error? foreach ($data as $key => $value) { if (is_null($record->model()->field($key))) { unset($data[$key]); } else { // Convert arrays to JSON // (Objects depend on __toString() for conversion) if (is_array($value)) { $data[$key] = json_encode($value); } } } $insert = new Dbi_Sql_Query_Insert(); //($record->model()->prefix() . $record->model()->name(), $data); //$this->_connection->query($insert->query()); //if ($this->_connection->errno) { // throw new Exception($this->_connection->error); //} $insert->table($record->model()->prefix() . $record->model()->name()); foreach ($data as $key => $value) { $insert->set($key, $value); } $stmt = $this->_execute($insert); $primary = $record->model()->index('primary'); if (is_array($primary) && count($primary['fields']) == 1) { $data[$primary['fields'][0]] = $this->_pdo->lastInsertId(); } // Return the data that was saved so Dbi_Record objects can update // automatically generated primary keys return $data; }
public function insert(Dbi_Record $record) { $collection = $this->_db->{$record->model()->name()}; $data = $record->getArray(!$this->enforceSchemas); $result = $collection->insert($data, true); /*$record->set('id', "{$data['_id']}"); $record->save();*/ $primary = $record->model()->index('primary'); if (!is_null($primary) && count($primary) == 1 && $primary[0] == 'id') { $data['id'] = "{$data['_id']}"; } return $data; }