From 3771af028204551fdd2074266e3ac35df8a693cf Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Tue, 7 Jan 2025 21:27:17 -0300 Subject: [PATCH] FIX: Index Respository --- app/common/Ideal/Repository.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/app/common/Ideal/Repository.php b/app/common/Ideal/Repository.php index 0edb118..4858ee9 100644 --- a/app/common/Ideal/Repository.php +++ b/app/common/Ideal/Repository.php @@ -25,7 +25,7 @@ abstract class Repository implements Define\Repository public function load(array $data_row): Define\Model { $model = $this->create($data_row); - $model->{$this->getKey()} = $data_row[$this->getKey()]; + $model->{$this->getIndex()} = $data_row[$this->getKey()]; return $model; } @@ -34,7 +34,7 @@ abstract class Repository implements Define\Repository $query = $this->connection->getQueryBuilder() ->delete()->from($this->getTable()) ->where("{$this->getKey()} = ?"); - $this->connection->execute($query, [$model->{$this->getKey()}]); + $this->connection->execute($query, [$model->{$this->getIndex()}]); } /** @@ -73,6 +73,18 @@ abstract class Repository implements Define\Repository { return $this->key; } + + protected string $index = 'id'; + public function setIndex(string $index): Repository + { + $this->index = $index; + return $this; + } + protected function getIndex(): string + { + return $this->index; + } + protected function parseData(Define\Model $model, ?array $data, Implement\Repository\MapperParser $data_map): Define\Model { if ($data === null) { @@ -146,9 +158,9 @@ abstract class Repository implements Define\Repository ->update($this->getTable()) ->set($columns_string) ->where("{$this->getKey()} = ?"); - $values []= $model->{$this->getKey()}; + $values []= $model->{$this->getIndex()}; $this->connection->execute($query, $values); - return $this->fetchById($model->{$this->getKey()}); + return $this->fetchById($model->{$this->getIndex()}); } protected function fetchOne(string $query, ?array $data = null): Define\Model {