本文整理汇总了PHP中Connection::lastInsertId方法的典型用法代码示例。如果您正苦于以下问题:PHP Connection::lastInsertId方法的具体用法?PHP Connection::lastInsertId怎么用?PHP Connection::lastInsertId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Connection
的用法示例。
在下文中一共展示了Connection::lastInsertId方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testLastInsertId
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());
}
示例2: replace
/**
* @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;
}
示例3: insert
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;
}
示例4: save
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;
}
示例5: insert
/**
* 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);
}
}
示例6: save
/**
* 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);
}
}