diff --git a/HasCount.php b/HasCount.php index 5cccd39..33e53e3 100644 --- a/HasCount.php +++ b/HasCount.php @@ -34,7 +34,8 @@ class HasCount extends HasBase */ public function get(): array|ModelInterface|null { - return $this->_relation->count($this->model::className(), $this->value); + $key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value; + return $this->_relation->count($key); } } diff --git a/HasMany.php b/HasMany.php index e6f238d..086738c 100644 --- a/HasMany.php +++ b/HasMany.php @@ -42,6 +42,7 @@ class HasMany extends HasBase */ public function get(): array|Collection|null { - return $this->_relation->get($this->model::className(), $this->value); + $key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value; + return $this->_relation->get($key); } } diff --git a/HasOne.php b/HasOne.php index f1ebd42..9fa334a 100644 --- a/HasOne.php +++ b/HasOne.php @@ -41,6 +41,7 @@ class HasOne extends HasBase */ public function get(): array|ModelInterface|null { - return $this->_relation->first($this->model::className(), $this->value); + $key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value; + return $this->_relation->first($key); } } diff --git a/Relation.php b/Relation.php index c249075..621862b 100644 --- a/Relation.php +++ b/Relation.php @@ -41,19 +41,16 @@ class Relation extends Component /** - * @param string $identification - * @param $localValue + * @param string $_identification * @return mixed - * @throws Exception */ - public function first(string $identification, $localValue): mixed + public function first(string $_identification): mixed { - $_identification = $identification . '_first_' . $localValue; if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) { return $this->_relations[$_identification]; } - $activeModel = $this->_query[$identification]->first(); + $activeModel = $this->_query[$_identification]->first(); if (empty($activeModel)) { return null; } @@ -63,19 +60,16 @@ class Relation extends Component /** - * @param string $identification - * @param $localValue + * @param string $_identification * @return mixed - * @throws Exception */ - public function count(string $identification, $localValue): mixed + public function count(string $_identification): mixed { - $_identification = $identification . '_count_' . $localValue; if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) { return $this->_relations[$_identification]; } - $activeModel = $this->_query[$identification]->count(); + $activeModel = $this->_query[$_identification]->count(); if (empty($activeModel)) { return null; } @@ -85,22 +79,16 @@ class Relation extends Component /** - * @param string $identification - * @param $localValue + * @param string $_identification * @return mixed */ - public function get(string $identification, $localValue): mixed + public function get(string $_identification): mixed { - if (is_array($localValue)) { - $_identification = $identification . '_get_' . implode('_', $localValue); - } else { - $_identification = $identification . '_get_' . $localValue; - } if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) { return $this->_relations[$_identification]; } - $activeModel = $this->_query[$identification]->get(); + $activeModel = $this->_query[$_identification]->get(); if (empty($activeModel)) { return $activeModel; } diff --git a/Traits/HasBase.php b/Traits/HasBase.php index 597e326..ecfae41 100644 --- a/Traits/HasBase.php +++ b/Traits/HasBase.php @@ -40,12 +40,12 @@ abstract class HasBase implements \Database\Traits\Relation /** * HasBase constructor. * @param ModelInterface $model - * @param $primaryId + * @param string $primaryId * @param $value * @param Relation $relation * @throws Exception */ - public function __construct(mixed $model, $primaryId, $value, Relation $relation) + public function __construct(mixed $model, public string $primaryId, $value, Relation $relation) { if (!class_exists($model)) { throw new Exception('Model must implement ' . $model); @@ -60,10 +60,9 @@ abstract class HasBase implements \Database\Traits\Relation $_model = $model::query()->where(['t1.' . $primaryId => $value]); } - $this->_relation = $relation->bindIdentification($model . '_' . $primaryId . '_' . $value, $_model); - + $this->value = is_array($value) ? json_encode($value,JSON_UNESCAPED_UNICODE) : $value; + $this->_relation = $relation->bindIdentification($model . '_' . $primaryId . '_' . $this->value, $_model); $this->model = $model; - $this->value = $value; }