From f87a3c386fc12c795b8f03a031f381f4f51e786c Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Thu, 29 Sep 2022 22:51:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HasCount.php | 16 ---------------- HasMany.php | 18 +----------------- HasOne.php | 20 ++------------------ Traits/HasBase.php | 41 +++++++++++++++++++++++++++++------------ 4 files changed, 32 insertions(+), 63 deletions(-) diff --git a/HasCount.php b/HasCount.php index c8f354e..f5e6945 100644 --- a/HasCount.php +++ b/HasCount.php @@ -13,22 +13,6 @@ use Exception; class HasCount extends HasBase { - /** - * @param $name - * @param $arguments - * @return static - */ - public function __call($name, $arguments) - { - if (!method_exists($this, $name)) { - $key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value; - $this->_relation->getQuery($key)->$name(...$arguments); - } else { - call_user_func([$this, $name], ...$arguments); - } - return $this; - } - /** * @return array|null|ModelInterface * @throws Exception diff --git a/HasMany.php b/HasMany.php index 4e5dc3b..a3812b1 100644 --- a/HasMany.php +++ b/HasMany.php @@ -21,22 +21,6 @@ use Exception; class HasMany extends HasBase { - /** - * @param $name - * @param $arguments - * @return static - */ - public function __call($name, $arguments) - { - if (!method_exists($this, $name)) { - $key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value; - $this->_relation->getQuery($key)->$name(...$arguments); - } else { - call_user_func([$this, $name], ...$arguments); - } - return $this; - } - /** * @return array|null|Collection * @throws Exception @@ -44,6 +28,6 @@ class HasMany extends HasBase public function get(): array|Collection|null { $key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value; - return $this->_relation->get($key); + return $this->_relation->get(md5($key)); } } diff --git a/HasOne.php b/HasOne.php index d2acfb8..6805e2f 100644 --- a/HasOne.php +++ b/HasOne.php @@ -19,23 +19,7 @@ use Exception; */ class HasOne extends HasBase { - - /** - * @param $name - * @param $arguments - * @return static - */ - public function __call($name, $arguments) - { - if (!method_exists($this, $name)) { - $key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value; - $this->_relation->getQuery($key)->$name(...$arguments); - } else { - call_user_func([$this, $name], ...$arguments); - } - return $this; - } - + /** * @return array|null|ModelInterface * @throws Exception @@ -43,6 +27,6 @@ class HasOne extends HasBase public function get(): array|ModelInterface|null { $key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value; - return $this->_relation->first($key); + return $this->_relation->first(md5($key)); } } diff --git a/Traits/HasBase.php b/Traits/HasBase.php index ecfae41..9753b30 100644 --- a/Traits/HasBase.php +++ b/Traits/HasBase.php @@ -22,21 +22,21 @@ use Exception; */ abstract class HasBase implements \Database\Traits\Relation { - + /** @var ModelInterface|Collection */ protected Collection|ModelInterface $data; - + /** * @var ModelInterface */ protected mixed $model; - - protected mixed $value = []; - - + + protected mixed $value = 0; + + /** @var Relation $_relation */ protected Relation $_relation; - + /** * HasBase constructor. * @param ModelInterface $model @@ -59,13 +59,30 @@ abstract class HasBase implements \Database\Traits\Relation } else { $_model = $model::query()->where(['t1.' . $primaryId => $value]); } - - $this->value = is_array($value) ? json_encode($value,JSON_UNESCAPED_UNICODE) : $value; - $this->_relation = $relation->bindIdentification($model . '_' . $primaryId . '_' . $this->value, $_model); + + $this->value = is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value; + $this->_relation = $relation->bindIdentification(md5($model . '_' . $primaryId . '_' . $this->value), $_model); $this->model = $model; } - - + + /** + * @param $name + * @param $arguments + * @return static + */ + public function __call($name, $arguments) + { + if (!method_exists($this, $name)) { + $key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value; + var_dump($this->model, $this->primaryId, $this->value); + $this->_relation->getQuery(md5($key))->$name(...$arguments); + } else { + call_user_func([$this, $name], ...$arguments); + } + return $this; + } + + /** * @param $name * @return mixed