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

View File

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

View File

@@ -194,25 +194,24 @@ class Query
* @access public * @access public
* @param string $sql sql指令 * @param string $sql sql指令
* @param array $bind 参数绑定 * @param array $bind 参数绑定
* @param boolean $getLastInsID 是否获取自增ID
* @param boolean $sequence 自增序列名
* @return int * @return int
* @throws BindParamException * @throws BindParamException
* @throws PDOException * @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 * 获取最近插入的ID
* @access public * @access public
* @param string $sequence 自增序列名
* @return string * @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 * @access public
* @param mixed $data 数据 * @param mixed $data 数据
* @param boolean $replace 是否replace * @param boolean $replace 是否replace
* @param boolean $getLastInsID 是否获取自增ID * @param boolean $getLastInsID 返回自增主键
* @param string $sequence 自增序列名 * @param string $sequence 自增序列名
* @return integer|string * @return integer|string
*/ */
@@ -1683,9 +1682,14 @@ class Query
// 获取实际执行的SQL语句 // 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $bind); 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;
} }
/** /**