mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-08-30 12:45:32 +08:00
改进分布式写入数据后及时读取的问题
This commit is contained in:
@@ -116,6 +116,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
*/
|
||||
protected static $initialized = [];
|
||||
|
||||
/**
|
||||
* 是否从主库读取(主从分布式有效)
|
||||
* @var bool
|
||||
*/
|
||||
protected static $readMaster = false;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* @access public
|
||||
@@ -171,6 +177,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
$this->initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否从主库读取数据(主从分布有效)
|
||||
* @access public
|
||||
* @param bool $master 是否从主库读取
|
||||
* @return void
|
||||
*/
|
||||
public function readMaster($master)
|
||||
{
|
||||
static::$readMaster = $master;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建模型的查询对象
|
||||
* @access protected
|
||||
@@ -194,6 +211,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||
$queryClass = $this->query ?: $con->getConfig('query');
|
||||
$query = new $queryClass($con, $this);
|
||||
|
||||
if (static::$readMaster) {
|
||||
$query->master(true);
|
||||
}
|
||||
|
||||
// 设置当前数据表和模型名
|
||||
if (!empty($this->table)) {
|
||||
$query->setTable($this->table);
|
||||
|
||||
Reference in New Issue
Block a user