From 44f5bf345b7b5b2e08f7110924ea4cd515f57230 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Mon, 31 May 2021 08:43:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=A8=A1=E5=9E=8B=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=85=B3=E8=81=94=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/catcher/base/CatchModel.php | 13 +++- extend/catcher/traits/db/WithTrait.php | 84 ++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 extend/catcher/traits/db/WithTrait.php diff --git a/extend/catcher/base/CatchModel.php b/extend/catcher/base/CatchModel.php index 0f58523..527b293 100644 --- a/extend/catcher/base/CatchModel.php +++ b/extend/catcher/base/CatchModel.php @@ -6,6 +6,7 @@ namespace catcher\base; use catcher\CatchQuery; use catcher\traits\db\BaseOptionsTrait; use catcher\traits\db\RewriteTrait; +use catcher\traits\db\WithTrait; use think\model\concern\SoftDelete; use catcher\traits\db\ScopeTrait; @@ -17,7 +18,7 @@ use catcher\traits\db\ScopeTrait; */ abstract class CatchModel extends \think\Model { - use SoftDelete, BaseOptionsTrait, ScopeTrait, RewriteTrait; + use SoftDelete, BaseOptionsTrait, ScopeTrait, RewriteTrait, WithTrait; protected $createTime = 'created_at'; @@ -47,4 +48,14 @@ abstract class CatchModel extends \think\Model { return property_exists($this, 'field') && in_array($field, $this->field); } + + public function __construct(array $data = []) + { + parent::__construct($data); + + + if (method_exists($this, 'autoWithRelation')) { + $this->autoWithRelation(); + } + } } diff --git a/extend/catcher/traits/db/WithTrait.php b/extend/catcher/traits/db/WithTrait.php new file mode 100644 index 0000000..b33ddc7 --- /dev/null +++ b/extend/catcher/traits/db/WithTrait.php @@ -0,0 +1,84 @@ + + * @copyright By CatchAdmin + * @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt + */ +namespace catcher\traits\db; + +trait WithTrait +{ + /** + * + * @time 2021年05月28日 + * @return mixed + */ + protected function autoWithRelation() + { + if (property_exists($this, 'globalScope')) { + array_push($this->globalScope, 'withRelation'); + + } + $this->scope('scopeWith'); + if (property_exists($this, 'with')) { + + + return $this->with($this->with); + + } + + return $this; + } + + /** + * + * @time 2021年05月28日 + * @param $query + * @return void + */ + public function scopeWithRelation($query) + { + if (property_exists($this, 'with') && !empty($this->with)) { + $query->with($this->with); + } + } + + /** + * + * @time 2021年05月28日 + * @param string $withRelation + * @return $this + */ + public function withoutRelation(string $withRelation) + { + $withes = $this->getOptions('with'); + + foreach ($withes as $k => $item) { + if ($item === $withRelation) { + unset($withes[$k]); + break; + } + } + + return $this->setOption('with', $withes); + + } + + /** + * + * @time 2021年05月28日 + * @param string $withRelation + * @return $this + */ + public function withOnlyRelation(string $withRelation) + { + return $this->with($withRelation); + } + +} \ No newline at end of file