query类增加insertGetId方法 模型类的save方法 新增的时候 返回自增id

This commit is contained in:
thinkphp
2016-04-28 23:10:04 +08:00
parent d8ac3012fe
commit 8eec4c5eb2
4 changed files with 25 additions and 6 deletions

View File

@@ -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) {

View File

@@ -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);
}
/**