From 11f6419b4f454926df82803b2bdf2017671025ce Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Sun, 21 Jun 2020 10:50:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E8=B1=A1scope=20trait?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/system/model/SensitiveWord.php | 17 ----------- extend/catcher/base/CatchModel.php | 3 +- extend/catcher/traits/db/BaseOptionsTrait.php | 2 -- extend/catcher/traits/db/ScopeTrait.php | 30 +++++++++++++++++-- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/catch/system/model/SensitiveWord.php b/catch/system/model/SensitiveWord.php index a765fe3..dbf61a8 100644 --- a/catch/system/model/SensitiveWord.php +++ b/catch/system/model/SensitiveWord.php @@ -40,21 +40,4 @@ class SensitiveWord extends CatchModel { return $query->whereLike('word', $value); } - - - /** - * 创建人 - * - * @time 2020年06月17日 - * @param $query - * @return mixed - */ - public function scopeCreator($query) - { - return $query->addSelectSub(function (){ - $user = app(Users::class); - return $user->whereColumn($this->getTable() . '.creator_id', $user->getTable() . '.id') - ->field('username'); - }, 'creator'); - } } \ No newline at end of file diff --git a/extend/catcher/base/CatchModel.php b/extend/catcher/base/CatchModel.php index a711892..77626eb 100644 --- a/extend/catcher/base/CatchModel.php +++ b/extend/catcher/base/CatchModel.php @@ -5,6 +5,7 @@ use catcher\CatchQuery; use catcher\traits\db\BaseOptionsTrait; use catcher\traits\db\TransTrait; use think\model\concern\SoftDelete; +use catcher\traits\db\ScopeTrait; /** * @@ -14,7 +15,7 @@ use think\model\concern\SoftDelete; */ abstract class CatchModel extends \think\Model { - use SoftDelete, TransTrait, BaseOptionsTrait; + use SoftDelete, TransTrait, BaseOptionsTrait, ScopeTrait; protected $createTime = 'created_at'; diff --git a/extend/catcher/traits/db/BaseOptionsTrait.php b/extend/catcher/traits/db/BaseOptionsTrait.php index ae89123..69b8557 100644 --- a/extend/catcher/traits/db/BaseOptionsTrait.php +++ b/extend/catcher/traits/db/BaseOptionsTrait.php @@ -2,8 +2,6 @@ namespace catcher\traits\db; -use catchAdmin\permissions\model\Users; - trait BaseOptionsTrait { /** diff --git a/extend/catcher/traits/db/ScopeTrait.php b/extend/catcher/traits/db/ScopeTrait.php index 4a7f3a6..4eb097d 100644 --- a/extend/catcher/traits/db/ScopeTrait.php +++ b/extend/catcher/traits/db/ScopeTrait.php @@ -1,10 +1,36 @@ * @copyright By CatchAdmin * @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt - */ \ No newline at end of file + */ +namespace catcher\traits\db; + +use catchAdmin\permissions\model\Users; + +trait ScopeTrait +{ + /** + * 创建人 + * + * @time 2020年06月17日 + * @param $query + * @return mixed + */ + public function scopeCreator($query) + { + if (in_array('creator_id', $this->field)) { + return $query->addSelectSub(function () { + $user = app(Users::class); + return $user->whereColumn($this->getTable() . '.creator_id', $user->getTable() . '.id') + ->field('username'); + }, 'creator'); + } + + return $query; + } +} \ No newline at end of file