抽象scope trait

This commit is contained in:
JaguarJack 2020-06-21 10:50:05 +08:00
parent 448154f363
commit 11f6419b4f
4 changed files with 30 additions and 22 deletions

View File

@ -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');
}
}

View File

@ -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';

View File

@ -2,8 +2,6 @@
namespace catcher\traits\db;
use catchAdmin\permissions\model\Users;
trait BaseOptionsTrait
{
/**

View File

@ -1,6 +1,6 @@
<?php
/**
* @filename scopeTrait.php
* @filename ScopeTrait.php
* @createdAt 2020/6/21
* @project https://github.com/yanwenwu/catch-admin
* @document http://doc.catchadmin.com
@ -8,3 +8,29 @@
* @copyright By CatchAdmin
* @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt
*/
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;
}
}