diff --git a/HasCount.php b/HasCount.php index 778e44d..270dea8 100644 --- a/HasCount.php +++ b/HasCount.php @@ -19,8 +19,7 @@ class HasCount extends HasBase */ public function get(): array|ModelInterface|null { - $key = $this->model . '_' . $this->primaryId . '_' . $this->value; - return $this->_relation->count($key); + return $this->_relation->count($this->reKey()); } } diff --git a/HasMany.php b/HasMany.php index a92dd79..7687a22 100644 --- a/HasMany.php +++ b/HasMany.php @@ -27,7 +27,6 @@ class HasMany extends HasBase */ public function get(): array|Collection|null { - $key = $this->model . '_' . $this->primaryId . '_' . $this->value; - return $this->_relation->get(md5($key)); + return $this->_relation->get($this->reKey()); } } diff --git a/HasOne.php b/HasOne.php index ea25056..f3d7de9 100644 --- a/HasOne.php +++ b/HasOne.php @@ -26,7 +26,6 @@ class HasOne extends HasBase */ public function get(): array|ModelInterface|null { - $key = $this->model . '_' . $this->primaryId . '_' . $this->value; - return $this->_relation->first(md5($key)); + return $this->_relation->first($this->reKey()); } } diff --git a/Relation.php b/Relation.php index 621862b..0712510 100644 --- a/Relation.php +++ b/Relation.php @@ -6,6 +6,7 @@ namespace Database; use Exception; use Kiri\Abstracts\Component; +use Kiri\Context; /** * Class Relation @@ -46,16 +47,14 @@ class Relation extends Component */ public function first(string $_identification): mixed { - if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) { - return $this->_relations[$_identification]; + if (Context::hasContext($_identification)) { + return Context::getContext($_identification); } - $activeModel = $this->_query[$_identification]->first(); if (empty($activeModel)) { return null; } - - return $this->_relations[$_identification] = $activeModel; + return Context::setContext($_identification, $activeModel); } @@ -65,16 +64,14 @@ class Relation extends Component */ public function count(string $_identification): mixed { - if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) { - return $this->_relations[$_identification]; + if (Context::hasContext($_identification)) { + return Context::getContext($_identification); } - $activeModel = $this->_query[$_identification]->count(); if (empty($activeModel)) { return null; } - - return $this->_relations[$_identification] = $activeModel; + return Context::setContext($_identification, $activeModel); } @@ -84,16 +81,14 @@ class Relation extends Component */ public function get(string $_identification): mixed { - if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) { - return $this->_relations[$_identification]; + if (Context::hasContext($_identification)) { + return Context::getContext($_identification); } - $activeModel = $this->_query[$_identification]->get(); if (empty($activeModel)) { return $activeModel; } - - return $this->_relations[$_identification] = $activeModel; + return Context::setContext($_identification, $activeModel); } } diff --git a/Traits/HasBase.php b/Traits/HasBase.php index e07b672..2e1489d 100644 --- a/Traits/HasBase.php +++ b/Traits/HasBase.php @@ -74,8 +74,7 @@ abstract class HasBase implements \Database\Traits\Relation { if (!method_exists($this, $name)) { $key = $this->model . '_' . $this->primaryId . '_' . $this->value; - var_dump($this->model, $this->primaryId, $this->value); - $this->_relation->getQuery(md5($key))->$name(...$arguments); + $this->_relation->getQuery($this->reKey())->$name(...$arguments); } else { call_user_func([$this, $name], ...$arguments); } @@ -83,6 +82,15 @@ abstract class HasBase implements \Database\Traits\Relation } + /** + * @return string + */ + protected function reKey(): string + { + return md5($this->model . '_' . $this->primaryId . '_' . $this->value); + } + + /** * @param $name * @return mixed