diff --git a/library/think/Model.php b/library/think/Model.php index edd65bf4..692ace0e 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -100,7 +100,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 验证失败是否抛出异常 protected $failException = false; // 全局查询范围 - protected static $useGlobalScope = true; + protected $useGlobalScope = true; /** * 初始化过的模型. @@ -1116,8 +1116,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public static function useGlobalScope($use) { - $model = new static(); - static::$useGlobalScope = $use; + $model = new static(); + $model->useGlobalScope = $use; return $model; } @@ -1341,8 +1341,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { $query = $this->db(); // 全局作用域 - if (static::$useGlobalScope && method_exists($this, 'base')) { - call_user_func_array('static::base', [ & $query]); + if ($this->useGlobalScope && method_exists($this, 'base')) { + call_user_func_array([$this, 'base'], [ & $query]); } if (method_exists($this, 'scope' . $method)) { // 动态调用命名范围 @@ -1358,10 +1358,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public static function __callStatic($method, $params) { $query = self::getDb(); - $model = get_called_class(); + $model = new static(); // 全局作用域 - if (static::$useGlobalScope && method_exists($model, 'base')) { - call_user_func_array('static::base', [ & $query]); + if ($model->useGlobalScope && method_exists($model, 'base')) { + call_user_func_array([$model, 'base'], [ & $query]); } return call_user_func_array([$query, $method], $params); } diff --git a/library/traits/model/SoftDelete.php b/library/traits/model/SoftDelete.php index c2c0dfe9..cc279955 100644 --- a/library/traits/model/SoftDelete.php +++ b/library/traits/model/SoftDelete.php @@ -12,7 +12,8 @@ trait SoftDelete */ public function trashed() { - if (!empty($this->data[static::$deleteTime])) { + $field = $this->getDeleteTimeField(); + if (!empty($this->data[$field])) { return true; } return false; @@ -37,7 +38,8 @@ trait SoftDelete public static function onlyTrashed() { $model = new static(); - return $model->db()->where(static::$deleteTime, 'exp', 'is not null'); + $field = $this->getDeleteTimeField(); + return $model->db()->where($field, 'exp', 'is not null'); } /** @@ -51,10 +53,9 @@ trait SoftDelete if (false === $this->trigger('before_delete', $this)) { return false; } - - if (static::$deleteTime && !$force) { + $name = $this->getDeleteTimeField(); + if (!$force) { // 软删除 - $name = static::$deleteTime; $this->change[] = $name; $this->data[$name] = $this->autoWriteTimestamp($name); $result = $this->isUpdate()->save(); @@ -106,12 +107,10 @@ trait SoftDelete */ public function restore($where = []) { - if (static::$deleteTime) { - // 恢复删除 - $name = static::$deleteTime; - return $this->isUpdate()->save([$name => null], $where); - } - return false; + $name = $this->getDeleteTimeField(); + // 恢复删除 + return $this->isUpdate()->save([$name => null], $where); + } /** @@ -120,14 +119,28 @@ trait SoftDelete * @param \think\db\Query $query 查询对象 * @return void */ - protected static function base($query) + protected function base($query) { - if (static::$deleteTime) { - $query->where(!empty(static::$tableAlias) ? - static::$tableAlias . '.' . static::$deleteTime : - static::$deleteTime, - 'null'); - } + $field = $this->getDeleteTimeField(true); + $query->where($field, 'null'); } + /** + * 获取软删除字段 + * @access public + * @param bool $read 是否查询操作 写操作的时候会自动去掉表别名 + * @return integer + */ + protected function getDeleteTimeField($read = false) + { + if (isset($this->deleteTime)) { + $field = $this->deleteTime; + } else { + $field = 'delete_time'; + } + if (!$read && strpos($field, '.')) { + list($alias, $field) = explode('.', $field); + } + return $field; + } }