From a0b1a75d92f8a4c8487b6630dcc3d6ea11994108 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 30 Sep 2016 18:14:57 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E7=9A=84useGlobalScope=20?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E6=94=B9=E4=B8=BA=E5=8A=A8=E6=80=81=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=20=E5=85=A8=E5=B1=80=E6=9F=A5=E8=AF=A2=E8=8C=83?= =?UTF-8?q?=E5=9B=B4=E6=96=B9=E6=B3=95base=E6=94=B9=E4=B8=BA=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E6=96=B9=E6=B3=95=20=E8=BD=AF=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=20deleteTime=E5=B1=9E=E6=80=A7=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=8A=A8=E6=80=81=E5=B1=9E=E6=80=A7=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=B8=94=E6=B2=A1=E6=9C=89=E5=AE=9A=E4=B9=89=E7=9A=84=E8=AF=9D?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=B8=BAdelete=5Ftime=20=E5=8F=96=E6=B6=88ta?= =?UTF-8?q?bleAlias=E5=B1=9E=E6=80=A7=E5=AE=9A=E4=B9=89=20=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E5=8F=AF=E4=BB=A5=E5=9C=A8deleteTime=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E9=87=8C=E9=9D=A2=E5=AE=9A=E4=B9=89=E5=88=AB=E5=90=8D?= =?UTF-8?q?.=E5=AD=97=E6=AE=B5=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 16 +++++----- library/traits/model/SoftDelete.php | 49 ++++++++++++++++++----------- 2 files changed, 39 insertions(+), 26 deletions(-) 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; + } }