From 4183edc3b06c4b6701a1b938afdac5b14b553ca1 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 9 Apr 2016 00:18:26 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E5=AE=8C=E5=96=84scope=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 1121261f..615721cf 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -614,12 +614,28 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $result; } - // 命名范围 + /** + * 命名范围 + * @access public + * @param string|Closure $name 命名范围名称 逗号分隔 + * @return Db + */ public static function scope($name) { - $model = new static(); - $method = 'scope' . $name; - return $model->$method(); + $model = new static(); + $class = self::db(); + if ($name instanceof \Closure) { + call_user_func_array($name, [ & $class]); + } else { + $names = explode(',', $name); + foreach ($names as $scope) { + $method = 'scope' . $scope; + if (method_exists($model, $method)) { + $model->$method($class); + } + } + } + return $class; } // 解析模型的完整命名空间