public function testLastInsertId() { $this->createTable(); $this->connection->query("INSERT INTO cat (name, colour) VALUES (?, ?)", array('Nennek', 'black')); $this->assertEquals(3, $this->connection->lastInsertId('cat')); $this->assertEquals(3, $this->connection->lastInsertId()); $this->connection->query("INSERT INTO cat (name, colour) VALUES (?, ?)", array('Misia', 'white-black-gray stripes')); $this->assertEquals(4, $this->connection->lastInsertId()); }
/** * @param Model $model * @return Model|null * @throws Exception */ public function replace(Model $model) { $rtn = null; $data = $model->toArray(); $modified = $model->getModified(); $cols = []; $values = []; $params = []; foreach ($modified as $key) { $cols[] = $key; $values[] = ':' . $key; $params[':' . $key] = $data[$key]; } if (count($cols)) { $cols = '`' . implode('`, `', $cols) . '`'; $vals = implode(', ', $values); $query = "REPLACE INTO `{$this->table}` ({$cols}) VALUES ({$vals});"; $stmt = $this->connection->prepare($query); if ($stmt->execute($params)) { $modelId = !empty($data[$this->key]) ? $data[$this->key] : $this->connection->lastInsertId(); $this->cacheSet($data[$this->key], null); $rtn = $this->getByPrimaryKey($modelId, 'write'); $this->cacheSet($modelId, $rtn); } } return $rtn; }
public static function insert($attributes) { $sql = 'INSERT INTO ' . static::table(); $sql .= ' (' . implode(',', array_keys($attributes)) . ') VALUES ('; $sql .= implode(',', array_fill(0, count($attributes), '?')) . ')'; $statement = Connection::prepare($sql); $statement->execute(array_values($attributes)); $id = Connection::lastInsertId(); $attributes['id'] = $id; $cls = static::className(); $model = new $cls(); $model->setAttributes($attributes); return $model; }
public function save() { $db = new Connection(); $table = strtolower(get_class($this)); $prefixe = substr($table, 0, 3) . "_"; $props = get_object_vars($this); unset($props["id"]); foreach ($props as $prop => $value) { $column[] = $prefixe . $prop; $prop = ":" . $prop; $params[] = $prop; } $query = "INSERT INTO " . $table . "(" . implode(',', $column) . ") VALUES(" . implode(',', $params) . ")"; $request = $db->prepare($query); $request->execute($props); $id = $db->lastInsertId(); $db = null; return $id; }
/** * Inserts row in a table. * @param mixed array($column => $value)|Traversable for single row insert or Selection|string for INSERT ... SELECT * @return TableRow or FALSE in case of an error or number of affected rows for INSERT ... SELECT */ public function insert($data) { if ($data instanceof TableSelection) { $data = $data->getSql(); } elseif ($data instanceof Traversable) { $data = iterator_to_array($data); } $return = $this->connection->query($this->sqlBuilder->buildInsertQuery(), $data); $this->checkReferenced = TRUE; if (!is_array($data)) { return $return->rowCount(); } if (!isset($data[$this->primary]) && ($id = $this->connection->lastInsertId($this->getPrimarySequence()))) { $data[$this->primary] = $id; return $this->rows[$id] = $this->createRow($data); } else { return $this->createRow($data); } }
/** * Save row to table "$table". $params is a map of values. Key is a table field name. Value is a value. * When id = 0 row is inserted. When id <> 0 then row is updated. * Primary key should'n be inside $params array. * @param $table * @param array $params * @param int $id * @param string $idColumn * @return int */ public function save($table, array $params, $id = 0, $idColumn = 'id') { if (!is_string($table) || !strlen($table)) { throw new \InvalidArgumentException("Table name must be a string with length greather than 1."); } // if array is assoc if (!is_array($params) || 0 === count(array_diff_key($params, array_keys(array_keys($params))))) { throw new \InvalidArgumentException('Second parameter must be an associative array!'); } $query = ''; if ($id) { $query = $this->strategy->update($table, $params, $idColumn); $params[] = $id; $this->connection->query($query, $params); return $id; } else { $query = $this->strategy->insert($table, $params); $this->connection->query($query, $params); return $this->connection->lastInsertId($table, $idColumn); } }