Skip to content

Commit

Permalink
改进查询范围的执行
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Aug 5, 2024
1 parent ea880c1 commit 265dfe9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
4 changes: 1 addition & 3 deletions src/db/BaseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,8 @@ public function __call(string $method, array $args)

if ($this->model && method_exists($this->model, 'scope' . $method)) {
// 动态调用命名范围
$call = 'scope' . $method;
array_unshift($args, $this);

$this->options['scope'][$method] = [$call, $args];
$this->options['scope'][$method] = [[$this->model, 'scope' . $method], $args];

return $this;
}
Expand Down
16 changes: 7 additions & 9 deletions src/db/concern/ModelRelationQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public function scope($scope, ...$args)
array_unshift($args, $this);

if ($scope instanceof Closure) {
call_user_func_array($scope, $args);

$this->options['scope'][] = [$scope, $args];
return $this;
}

Expand All @@ -123,8 +122,9 @@ public function scope($scope, ...$args)
// 检查模型类的查询范围方法
foreach ($scope as $name) {
$method = 'scope' . trim($name);

$this->options['scope'][$name] = [$method, $args];
if (method_exists($this->model, $method)) {
$this->options['scope'][$name] = [[$this->model, $method], $args];
}
}
}

Expand All @@ -138,12 +138,10 @@ public function scope($scope, ...$args)
*/
protected function scopeQuery()
{
if ($this->model && !empty($this->options['scope'])) {
if (!empty($this->options['scope'])) {
foreach ($this->options['scope'] as $name => $val) {
[$method, $args] = $val;
if (method_exists($this->model, $method)) {
call_user_func_array([$this->model, $method], $args);
}
[$call, $args] = $val;
call_user_func_array($call, $args);
}
}

Expand Down

0 comments on commit 265dfe9

Please sign in to comment.