mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
改进分布式写入数据后及时读取的问题
This commit is contained in:
@@ -116,6 +116,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
protected static $initialized = [];
|
protected static $initialized = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否从主库读取(主从分布式有效)
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected static $readMaster = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造方法
|
* 构造方法
|
||||||
* @access public
|
* @access public
|
||||||
@@ -171,6 +177,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$this->initialize();
|
$this->initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否从主库读取数据(主从分布有效)
|
||||||
|
* @access public
|
||||||
|
* @param bool $master 是否从主库读取
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function readMaster($master)
|
||||||
|
{
|
||||||
|
static::$readMaster = $master;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建模型的查询对象
|
* 创建模型的查询对象
|
||||||
* @access protected
|
* @access protected
|
||||||
@@ -194,6 +211,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$queryClass = $this->query ?: $con->getConfig('query');
|
$queryClass = $this->query ?: $con->getConfig('query');
|
||||||
$query = new $queryClass($con, $this);
|
$query = new $queryClass($con, $this);
|
||||||
|
|
||||||
|
if (static::$readMaster) {
|
||||||
|
$query->master(true);
|
||||||
|
}
|
||||||
|
|
||||||
// 设置当前数据表和模型名
|
// 设置当前数据表和模型名
|
||||||
if (!empty($this->table)) {
|
if (!empty($this->table)) {
|
||||||
$query->setTable($this->table);
|
$query->setTable($this->table);
|
||||||
|
|||||||
@@ -90,6 +90,8 @@ abstract class Connection
|
|||||||
'master_num' => 1,
|
'master_num' => 1,
|
||||||
// 指定从服务器序号
|
// 指定从服务器序号
|
||||||
'slave_no' => '',
|
'slave_no' => '',
|
||||||
|
// 模型写入后自动读取主服务器
|
||||||
|
'read_master' => false,
|
||||||
// 是否严格检查字段是否存在
|
// 是否严格检查字段是否存在
|
||||||
'fields_strict' => true,
|
'fields_strict' => true,
|
||||||
// 数据返回类型
|
// 数据返回类型
|
||||||
@@ -402,13 +404,14 @@ abstract class Connection
|
|||||||
/**
|
/**
|
||||||
* 执行语句
|
* 执行语句
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $sql sql指令
|
* @param string $sql sql指令
|
||||||
* @param array $bind 参数绑定
|
* @param array $bind 参数绑定
|
||||||
|
* @param Query $query 查询对象
|
||||||
* @return int
|
* @return int
|
||||||
* @throws PDOException
|
* @throws PDOException
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function execute($sql, $bind = [])
|
public function execute($sql, $bind = [], Query $query = null)
|
||||||
{
|
{
|
||||||
$this->initConnect(true);
|
$this->initConnect(true);
|
||||||
if (!$this->linkID) {
|
if (!$this->linkID) {
|
||||||
@@ -447,6 +450,10 @@ abstract class Connection
|
|||||||
// 调试结束
|
// 调试结束
|
||||||
$this->debug(false);
|
$this->debug(false);
|
||||||
|
|
||||||
|
if ($query && !empty($this->config['deploy']) && !empty($this->config['read_master'])) {
|
||||||
|
$query->setModelReadMaster(true);
|
||||||
|
}
|
||||||
|
|
||||||
$this->numRows = $this->PDOStatement->rowCount();
|
$this->numRows = $this->PDOStatement->rowCount();
|
||||||
return $this->numRows;
|
return $this->numRows;
|
||||||
} catch (\PDOException $e) {
|
} catch (\PDOException $e) {
|
||||||
@@ -744,7 +751,7 @@ abstract class Connection
|
|||||||
* @param array $sqlArray SQL批处理指令
|
* @param array $sqlArray SQL批处理指令
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function batchQuery($sqlArray = [], $bind = [])
|
public function batchQuery($sqlArray = [], $bind = [], Query $query = null)
|
||||||
{
|
{
|
||||||
if (!is_array($sqlArray)) {
|
if (!is_array($sqlArray)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -753,7 +760,7 @@ abstract class Connection
|
|||||||
$this->startTrans();
|
$this->startTrans();
|
||||||
try {
|
try {
|
||||||
foreach ($sqlArray as $sql) {
|
foreach ($sqlArray as $sql) {
|
||||||
$this->execute($sql, $bind);
|
$this->execute($sql, $bind, $query);
|
||||||
}
|
}
|
||||||
// 提交事务
|
// 提交事务
|
||||||
$this->commit();
|
$this->commit();
|
||||||
|
|||||||
@@ -140,6 +140,19 @@ class Query
|
|||||||
return $this->model;
|
return $this->model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置模型从主库读取数据
|
||||||
|
* @access public
|
||||||
|
* @param bool $master
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setModelReadMaster($master = true)
|
||||||
|
{
|
||||||
|
if ($this->model) {
|
||||||
|
$this->model->readMaster($master);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前的builder实例对象
|
* 获取当前的builder实例对象
|
||||||
* @access public
|
* @access public
|
||||||
@@ -238,7 +251,7 @@ class Query
|
|||||||
*/
|
*/
|
||||||
public function execute($sql, $bind = [])
|
public function execute($sql, $bind = [])
|
||||||
{
|
{
|
||||||
return $this->connection->execute($sql, $bind);
|
return $this->connection->execute($sql, $bind, $this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2204,7 +2217,7 @@ class Query
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 执行操作
|
// 执行操作
|
||||||
$result = 0 === $sql ? 0 : $this->execute($sql, $bind);
|
$result = 0 === $sql ? 0 : $this->execute($sql, $bind, $this);
|
||||||
if ($result) {
|
if ($result) {
|
||||||
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
|
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
|
||||||
$lastInsId = $this->getLastInsID($sequence);
|
$lastInsId = $this->getLastInsID($sequence);
|
||||||
@@ -2270,10 +2283,10 @@ class Query
|
|||||||
return $this->connection->getRealSql($sql, $bind);
|
return $this->connection->getRealSql($sql, $bind);
|
||||||
} elseif (is_array($sql)) {
|
} elseif (is_array($sql)) {
|
||||||
// 执行操作
|
// 执行操作
|
||||||
return $this->batchQuery($sql, $bind);
|
return $this->batchQuery($sql, $bind, $this);
|
||||||
} else {
|
} else {
|
||||||
// 执行操作
|
// 执行操作
|
||||||
return $this->execute($sql, $bind);
|
return $this->execute($sql, $bind, $this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2299,7 +2312,7 @@ class Query
|
|||||||
return $this->connection->getRealSql($sql, $bind);
|
return $this->connection->getRealSql($sql, $bind);
|
||||||
} else {
|
} else {
|
||||||
// 执行操作
|
// 执行操作
|
||||||
return $this->execute($sql, $bind);
|
return $this->execute($sql, $bind, $this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2366,7 +2379,7 @@ class Query
|
|||||||
Cache::clear($options['cache']['tag']);
|
Cache::clear($options['cache']['tag']);
|
||||||
}
|
}
|
||||||
// 执行操作
|
// 执行操作
|
||||||
$result = '' == $sql ? 0 : $this->execute($sql, $bind);
|
$result = '' == $sql ? 0 : $this->execute($sql, $bind, $this);
|
||||||
if ($result) {
|
if ($result) {
|
||||||
if (is_string($pk) && isset($where[$pk])) {
|
if (is_string($pk) && isset($where[$pk])) {
|
||||||
$data[$pk] = $where[$pk];
|
$data[$pk] = $where[$pk];
|
||||||
@@ -2838,7 +2851,7 @@ class Query
|
|||||||
Cache::clear($options['cache']['tag']);
|
Cache::clear($options['cache']['tag']);
|
||||||
}
|
}
|
||||||
// 执行操作
|
// 执行操作
|
||||||
$result = $this->execute($sql, $bind);
|
$result = $this->execute($sql, $bind, $this);
|
||||||
if ($result) {
|
if ($result) {
|
||||||
if (!is_array($data) && is_string($pk) && isset($key) && strpos($key, '|')) {
|
if (!is_array($data) && is_string($pk) && isset($key) && strpos($key, '|')) {
|
||||||
list($a, $val) = explode('|', $key);
|
list($a, $val) = explode('|', $key);
|
||||||
|
|||||||
Reference in New Issue
Block a user