mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
query类增加insertGetId方法 模型类的save方法 新增的时候 返回自增id
This commit is contained in:
@@ -207,7 +207,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
* @param array $where 更新条件
|
||||
* @return integer
|
||||
*/
|
||||
public function save($data = [], $where = [], $getInsertId = true)
|
||||
public function save($data = [], $where = [])
|
||||
{
|
||||
if (!empty($data)) {
|
||||
// 数据对象赋值
|
||||
@@ -270,12 +270,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
$result = self::db()->insert($this->data);
|
||||
|
||||
// 获取自动增长主键
|
||||
if ($result && $getInsertId) {
|
||||
if ($result) {
|
||||
$insertId = self::db()->getLastInsID();
|
||||
$pk = $this->getPk();
|
||||
if (is_string($pk) && $insertId) {
|
||||
$this->data[$pk] = $insertId;
|
||||
}
|
||||
$result = $insertId;
|
||||
}
|
||||
// 新增回调
|
||||
$this->trigger('after_insert', $this);
|
||||
|
||||
@@ -303,9 +303,10 @@ abstract class Connection
|
||||
* @param string $sql sql指令
|
||||
* @param array $bind 参数绑定
|
||||
* @param boolean $fetch 不执行只是获取SQL
|
||||
* @param boolean $getLastInsID 是否获取自增ID
|
||||
* @return integer
|
||||
*/
|
||||
public function execute($sql, $bind = [], $fetch = false)
|
||||
public function execute($sql, $bind = [], $fetch = false, $getLastInsID = false)
|
||||
{
|
||||
$this->initConnect(true);
|
||||
if (!$this->linkID) {
|
||||
@@ -338,6 +339,9 @@ abstract class Connection
|
||||
$this->numRows = $this->PDOStatement->rowCount();
|
||||
if (preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $sql)) {
|
||||
$this->lastInsID = $this->linkID->lastInsertId();
|
||||
if ($getLastInsID) {
|
||||
return $this->lastInsID;
|
||||
}
|
||||
}
|
||||
return $this->numRows;
|
||||
} catch (\PDOException $e) {
|
||||
|
||||
@@ -1094,16 +1094,29 @@ class Query
|
||||
* @access public
|
||||
* @param mixed $data 数据
|
||||
* @param boolean $replace 是否replace
|
||||
* @param boolean $getLastInsID 是否获取自增ID
|
||||
* @return integer
|
||||
*/
|
||||
public function insert(array $data, $replace = false)
|
||||
public function insert(array $data, $replace = false, $getLastInsID = false)
|
||||
{
|
||||
// 分析查询表达式
|
||||
$options = $this->parseExpress();
|
||||
// 生成SQL语句
|
||||
$sql = $this->builder()->insert($data, $options, $replace);
|
||||
// 执行操作
|
||||
return $this->connection->execute($sql, $this->getBind(), $options['fetch_sql']);
|
||||
return $this->connection->execute($sql, $this->getBind(), $options['fetch_sql'], $getLastInsID);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入记录并获取自增ID
|
||||
* @access public
|
||||
* @param mixed $data 数据
|
||||
* @param boolean $replace 是否replace
|
||||
* @return integer
|
||||
*/
|
||||
public function insertGetId(array $data, $replace = false)
|
||||
{
|
||||
return $this->insert($data, $replace, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -145,7 +145,7 @@ class Merge extends Model
|
||||
* @param array $where 更新条件
|
||||
* @return mixed
|
||||
*/
|
||||
public function save($data = [], $where = [], $getInsertId = true)
|
||||
public function save($data = [], $where = [])
|
||||
{
|
||||
if (!empty($data)) {
|
||||
// 数据对象赋值
|
||||
@@ -211,6 +211,7 @@ class Merge extends Model
|
||||
$data = $this->parseData($name, $this->data, true);
|
||||
self::db()->table($table)->strict(false)->insert($data);
|
||||
}
|
||||
$result = $insertId;
|
||||
}
|
||||
// 新增回调
|
||||
$this->trigger('after_insert', $this);
|
||||
|
||||
Reference in New Issue
Block a user