抽象scope trait
This commit is contained in:
parent
448154f363
commit
11f6419b4f
@ -40,21 +40,4 @@ class SensitiveWord extends CatchModel
|
|||||||
{
|
{
|
||||||
return $query->whereLike('word', $value);
|
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');
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -5,6 +5,7 @@ use catcher\CatchQuery;
|
|||||||
use catcher\traits\db\BaseOptionsTrait;
|
use catcher\traits\db\BaseOptionsTrait;
|
||||||
use catcher\traits\db\TransTrait;
|
use catcher\traits\db\TransTrait;
|
||||||
use think\model\concern\SoftDelete;
|
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
|
abstract class CatchModel extends \think\Model
|
||||||
{
|
{
|
||||||
use SoftDelete, TransTrait, BaseOptionsTrait;
|
use SoftDelete, TransTrait, BaseOptionsTrait, ScopeTrait;
|
||||||
|
|
||||||
protected $createTime = 'created_at';
|
protected $createTime = 'created_at';
|
||||||
|
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
namespace catcher\traits\db;
|
namespace catcher\traits\db;
|
||||||
|
|
||||||
use catchAdmin\permissions\model\Users;
|
|
||||||
|
|
||||||
trait BaseOptionsTrait
|
trait BaseOptionsTrait
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* @filename scopeTrait.php
|
* @filename ScopeTrait.php
|
||||||
* @createdAt 2020/6/21
|
* @createdAt 2020/6/21
|
||||||
* @project https://github.com/yanwenwu/catch-admin
|
* @project https://github.com/yanwenwu/catch-admin
|
||||||
* @document http://doc.catchadmin.com
|
* @document http://doc.catchadmin.com
|
||||||
@ -8,3 +8,29 @@
|
|||||||
* @copyright By CatchAdmin
|
* @copyright By CatchAdmin
|
||||||
* @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt
|
* @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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user