From 1dfe32bf3ced2a63b12eeae8f6a6673eae5e2a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Sat, 1 Apr 2023 18:50:44 +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 | 2 +- HasMany.php | 2 +- HasOne.php | 2 +- Traits/HasBase.php | 9 ++++++--- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/HasCount.php b/HasCount.php index 285d201..85acb1e 100644 --- a/HasCount.php +++ b/HasCount.php @@ -21,7 +21,7 @@ class HasCount extends HasBase public function get(): array|ModelInterface|null { $relation = Kiri::getDi()->get(Relation::class); - return $relation->count($this->name); + return $relation->getQuery($this->name)->count(); } } diff --git a/HasMany.php b/HasMany.php index ea68f3a..56053b1 100644 --- a/HasMany.php +++ b/HasMany.php @@ -29,6 +29,6 @@ class HasMany extends HasBase public function get(): array|Collection|null { $relation = Kiri::getDi()->get(Relation::class); - return $relation->get($this->name); + return $relation->getQuery($this->name)->get(); } } diff --git a/HasOne.php b/HasOne.php index 613a4e7..3804a38 100644 --- a/HasOne.php +++ b/HasOne.php @@ -28,6 +28,6 @@ class HasOne extends HasBase public function get(): array|ModelInterface|null { $relation = Kiri::getDi()->get(Relation::class); - return $relation->first($this->name); + return $relation->getQuery($this->name)->first(); } } diff --git a/Traits/HasBase.php b/Traits/HasBase.php index 07a2ef7..f2bac84 100644 --- a/Traits/HasBase.php +++ b/Traits/HasBase.php @@ -28,7 +28,7 @@ abstract class HasBase implements \Database\Traits\Relation { /** @var ModelInterface|Collection */ - protected Collection|ModelInterface $data; + protected mixed $data = null; /** * @var ModelInterface @@ -37,7 +37,7 @@ abstract class HasBase implements \Database\Traits\Relation protected mixed $value = 0; - + /** * HasBase constructor. @@ -71,6 +71,9 @@ abstract class HasBase implements \Database\Traits\Relation */ public function __get($name): mixed { - return $this->get(); + if ($this->data === null) { + $this->data = $this->get(); + } + return $this->data; } }