mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
改进全局查询范围
This commit is contained in:
@@ -147,9 +147,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
/**
|
/**
|
||||||
* 获取当前模型的数据库查询对象
|
* 获取当前模型的数据库查询对象
|
||||||
* @access public
|
* @access public
|
||||||
|
* @param bool $baseQuery 是否调用全局查询范围
|
||||||
* @return Query
|
* @return Query
|
||||||
*/
|
*/
|
||||||
public function db()
|
public function db($baseQuery = true)
|
||||||
{
|
{
|
||||||
$model = $this->class;
|
$model = $this->class;
|
||||||
if (!isset(self::$links[$model])) {
|
if (!isset(self::$links[$model])) {
|
||||||
@@ -169,6 +170,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
|
|
||||||
self::$links[$model] = $query;
|
self::$links[$model] = $query;
|
||||||
}
|
}
|
||||||
|
// 全局作用域
|
||||||
|
if ($baseQuery && method_exists($this, 'base')) {
|
||||||
|
call_user_func_array([$this, 'base'], [ & self::$links[$model]]);
|
||||||
|
}
|
||||||
// 返回当前模型的数据库查询对象
|
// 返回当前模型的数据库查询对象
|
||||||
return self::$links[$model];
|
return self::$links[$model];
|
||||||
}
|
}
|
||||||
@@ -1340,8 +1345,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
public function __call($method, $args)
|
public function __call($method, $args)
|
||||||
{
|
{
|
||||||
$query = $this->db();
|
$query = $this->db();
|
||||||
// 全局作用域
|
|
||||||
$this->baseQuery($query);
|
|
||||||
if (method_exists($this, 'scope' . $method)) {
|
if (method_exists($this, 'scope' . $method)) {
|
||||||
// 动态调用命名范围
|
// 动态调用命名范围
|
||||||
$method = 'scope' . $method;
|
$method = 'scope' . $method;
|
||||||
@@ -1356,20 +1359,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
public static function __callStatic($method, $params)
|
public static function __callStatic($method, $params)
|
||||||
{
|
{
|
||||||
$query = self::getDb();
|
$query = self::getDb();
|
||||||
$model = new static();
|
|
||||||
// 全局作用域
|
|
||||||
$model->baseQuery($query);
|
|
||||||
return call_user_func_array([$query, $method], $params);
|
return call_user_func_array([$query, $method], $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function baseQuery(&$query)
|
|
||||||
{
|
|
||||||
// 全局作用域
|
|
||||||
if ($this->useGlobalScope && method_exists($this, 'base')) {
|
|
||||||
call_user_func_array([$this, 'base'], [ & $query]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static function getDb()
|
protected static function getDb()
|
||||||
{
|
{
|
||||||
$model = get_called_class();
|
$model = get_called_class();
|
||||||
|
|||||||
@@ -661,9 +661,6 @@ class Relation
|
|||||||
public function __call($method, $args)
|
public function __call($method, $args)
|
||||||
{
|
{
|
||||||
if ($this->query) {
|
if ($this->query) {
|
||||||
$class = new $this->model;
|
|
||||||
// 全局作用域
|
|
||||||
$class->baseQuery($this->query);
|
|
||||||
switch ($this->type) {
|
switch ($this->type) {
|
||||||
case self::HAS_MANY:
|
case self::HAS_MANY:
|
||||||
if (isset($this->where)) {
|
if (isset($this->where)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user