改进关联查询的全局命名范围查询

This commit is contained in:
thinkphp
2016-09-30 22:11:29 +08:00
parent 77928649d7
commit 88dfa563b3
2 changed files with 13 additions and 6 deletions

View File

@@ -1341,9 +1341,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
{
$query = $this->db();
// 全局作用域
if ($this->useGlobalScope && method_exists($this, 'base')) {
call_user_func_array([$this, 'base'], [ & $query]);
}
$this->baseQuery($query);
if (method_exists($this, 'scope' . $method)) {
// 动态调用命名范围
$method = 'scope' . $method;
@@ -1360,12 +1358,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$query = self::getDb();
$model = new static();
// 全局作用域
if ($model->useGlobalScope && method_exists($model, 'base')) {
call_user_func_array([$model, 'base'], [ & $query]);
}
$model->baseQuery($query);
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()
{
$model = get_called_class();

View File

@@ -661,6 +661,9 @@ class Relation
public function __call($method, $args)
{
if ($this->query) {
$class = new $this->model;
// 全局作用域
$class->baseQuery($this->query);
switch ($this->type) {
case self::HAS_MANY:
if (isset($this->where)) {