Skip to content

Commit 01fd9b6

Browse files
committed
增加全局查询范围支持
1 parent b34b147 commit 01fd9b6

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/model/concern/DbConnect.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,36 @@ protected function query(Query $query) {}
181181
/**
182182
* 获取查询对象
183183
*
184+
* @param array|null $scope 设置不使用的全局查询范围
184185
* @return Query
185186
*/
186-
public function getQuery(): Query
187+
public function getQuery(array | null $scope = []): Query
187188
{
188189
$query = $this->db();
189-
190+
// 全局查询范围
191+
if (is_array($scope)) {
192+
$globalScope = array_diff($this->getOption('globalScope', []), $scope);
193+
$query->scope($globalScope);
194+
}
190195
// 执行扩展查询
191196
$this->query($query->suffix($this->getOption('suffix')));
192197
return $query;
193198
}
194199

200+
/**
201+
* 设置不使用的全局查询范围.
202+
*
203+
* @param array $scope 不启用的全局查询范围
204+
*
205+
* @return Query
206+
*/
207+
public static function withoutGlobalScope(?array $scope = null): Query
208+
{
209+
$model = new static();
210+
211+
return $model->getQuery($scope);
212+
}
213+
195214
public static function __callStatic($method, $args)
196215
{
197216
$model = new static();

0 commit comments

Comments
 (0)