mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-02 05:02:48 +08:00
model类完善scope方法 并支持动态调用 重写driver类的scope方法
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user