diff --git a/library/think/Model.php b/library/think/Model.php index 615721cf..238a5d50 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -554,7 +554,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public static function create($data = []) { $model = new static(); - return $model->insert($data); + return $model->isUpdate(false)->save($data); } /** @@ -620,18 +620,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param string|Closure $name 命名范围名称 逗号分隔 * @return Db */ - public static function scope($name) + public static function scope($name, $params = []) { $model = new static(); $class = self::db(); if ($name instanceof \Closure) { - call_user_func_array($name, [ & $class]); + call_user_func_array($name, [ & $class, $params]); } else { $names = explode(',', $name); foreach ($names as $scope) { $method = 'scope' . $scope; if (method_exists($model, $method)) { - $model->$method($class); + $model->$method($class, $params); } } } @@ -726,6 +726,20 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return self::$links[$model]; } + public function __call($method, $args) + { + if (method_exists($this, 'scope' . $method)) { + // 动态调用命名范围 + $method = 'scope' . $method; + $class = self::db(); + array_unshift($args, $class); + call_user_func_array([$this, $method], $args); + return $class; + } else { + throw new Exception(__CLASS__ . ':' . $method . ' method not exist'); + } + } + public static function __callStatic($method, $params) { return call_user_func_array([self::db(), $method], $params); diff --git a/library/think/db/Driver.php b/library/think/db/Driver.php index add300e5..474d19a4 100644 --- a/library/think/db/Driver.php +++ b/library/think/db/Driver.php @@ -132,9 +132,6 @@ abstract class Driver $name = Loader::parseName(substr($method, 10)); $where[$name] = $args[0]; return $this->where($where)->value($args[1]); - } elseif (isset($this->scope[$method])) { - // 命名范围的单独调用支持 - return $this->scope($method, $args[0]); } else { throw new Exception(__CLASS__ . ':' . $method . ' method not exist'); } @@ -964,39 +961,14 @@ abstract class Driver /** * 调用命名范围 * @access public - * @param mixed $scope 命名范围名称 支持多个 和直接定义 - * @param array $args 参数 - * @return Model + * @param Closure $scope 命名范围 闭包定义 + * @param mixed $args 参数 + * @return Db */ public function scope($scope = '', $args = null) { - if ('' === $scope) { - if (isset($this->scope['default'])) { - // 默认的命名范围 - $options = $this->scope['default']; - } else { - return $this; - } - } elseif (is_string($scope)) { - // 支持多个命名范围调用 用逗号分割 - $scopes = explode(',', $scope); - $options = []; - foreach ($scopes as $name) { - if (!isset($this->scope[$name])) { - continue; - } - $options = array_merge($options, $this->scope[$name]); - } - if (!empty($args) && is_array($args)) { - $options = array_merge($options, $args); - } - } else { - // 直接传入命名范围定义 - $options = $scope; - } - - if (is_array($options) && !empty($options)) { - $this->options = array_merge($this->options, array_change_key_case($options)); + if ($scope instanceof \Closure) { + call_user_func_array($scope, [ & $this, $args]); } return $this; }