Model类的save方法参数和返回值调整 返回值固定为影响的记录数 Connection类及Query类的execute方法和getLastInsID方法参数调整

This commit is contained in:
thinkphp
2016-08-15 19:17:32 +08:00
parent 2fcebcced7
commit 2beba43b2e
3 changed files with 26 additions and 30 deletions

View File

@@ -606,11 +606,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @access public
* @param array $data 数据
* @param array $where 更新条件
* @param bool $getId 新增的时候是否获取id
* @param bool $replace 是否replace
* @param string $sequence 自增序列名
* @return integer
*/
public function save($data = [], $where = [], $getId = true, $replace = false)
public function save($data = [], $where = [], $sequence = null)
{
if (!empty($data)) {
// 数据自动验证
@@ -703,16 +702,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return false;
}
$result = $this->db()->insert($this->data, $replace);
$result = $this->db()->insert($this->data);
// 获取自动增长主键
if ($result && $getId) {
$insertId = $this->db()->getLastInsID();
if ($result) {
$insertId = $this->db()->getLastInsID($sequence);
$pk = $this->getPk();
if (is_string($pk) && $insertId) {
$this->data[$pk] = $insertId;
}
$result = $insertId;
}
// 标记为更新
$this->isUpdate = true;
@@ -954,14 +952,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* 写入数据
* @access public
* @param array $data 数据数组
* @param bool $replace 是否replace
* @param bool $getId 是否返回自增主键
* @return $this
*/
public static function create($data = [], $replace = false, $getId = true)
public static function create($data = [])
{
$model = new static();
$model->isUpdate(false)->save($data, [], $getId, $replace);
$model->isUpdate(false)->save($data, []);
return $model;
}

View File

@@ -365,13 +365,11 @@ abstract class Connection
* @access public
* @param string $sql sql指令
* @param array $bind 参数绑定
* @param boolean $getLastInsID 是否获取自增ID
* @param string $sequence 自增序列名
* @return int
* @throws BindParamException
* @throws PDOException
*/
public function execute($sql, $bind = [], $getLastInsID = false, $sequence = null)
public function execute($sql, $bind = [])
{
$this->initConnect(true);
if (!$this->linkID) {
@@ -399,12 +397,6 @@ abstract class Connection
$this->debug(false);
$this->numRows = $this->PDOStatement->rowCount();
if (preg_match("/^\s*(INSERT\s+INTO|REPLACE\s+INTO)\s+/i", $sql)) {
$this->lastInsID = $this->linkID->lastInsertId($sequence);
if ($getLastInsID) {
return $this->lastInsID;
}
}
return $this->numRows;
} catch (\PDOException $e) {
throw new PDOException($e, $this->config, $this->queryStr);
@@ -702,10 +694,14 @@ abstract class Connection
/**
* 获取最近插入的ID
* @access public
* @param string $sequence 自增序列名
* @return string
*/
public function getLastInsID()
public function getLastInsID($sequence = null)
{
if (is_null($this->lastInsID)) {
$this->lastInsID = $this->linkID->lastInsertId($sequence);
}
return $this->lastInsID;
}

View File

@@ -194,25 +194,24 @@ class Query
* @access public
* @param string $sql sql指令
* @param array $bind 参数绑定
* @param boolean $getLastInsID 是否获取自增ID
* @param boolean $sequence 自增序列名
* @return int
* @throws BindParamException
* @throws PDOException
*/
public function execute($sql, $bind = [], $getLastInsID = false, $sequence = null)
public function execute($sql, $bind = [])
{
return $this->connection->execute($sql, $bind, $getLastInsID, $sequence);
return $this->connection->execute($sql, $bind);
}
/**
* 获取最近插入的ID
* @access public
* @param string $sequence 自增序列名
* @return string
*/
public function getLastInsID()
public function getLastInsID($sequence = null)
{
return $this->connection->getLastInsID();
return $this->connection->getLastInsID($sequence);
}
/**
@@ -1667,7 +1666,7 @@ class Query
* @access public
* @param mixed $data 数据
* @param boolean $replace 是否replace
* @param boolean $getLastInsID 是否获取自增ID
* @param boolean $getLastInsID 返回自增主键
* @param string $sequence 自增序列名
* @return integer|string
*/
@@ -1683,9 +1682,14 @@ class Query
// 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $bind);
}
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
// 执行操作
return $this->execute($sql, $bind, $getLastInsID, $sequence);
$result = $this->execute($sql, $bind);
if ($getLastInsID) {
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
return $this->getLastInsID($sequence);
}
return $result;
}
/**