FIX: Index Respository

This commit is contained in:
Juan Pablo Vial
2025-01-07 21:27:17 -03:00
parent 574918f1cc
commit 3771af0282

View File

@ -25,7 +25,7 @@ abstract class Repository implements Define\Repository
public function load(array $data_row): Define\Model public function load(array $data_row): Define\Model
{ {
$model = $this->create($data_row); $model = $this->create($data_row);
$model->{$this->getKey()} = $data_row[$this->getKey()]; $model->{$this->getIndex()} = $data_row[$this->getKey()];
return $model; return $model;
} }
@ -34,7 +34,7 @@ abstract class Repository implements Define\Repository
$query = $this->connection->getQueryBuilder() $query = $this->connection->getQueryBuilder()
->delete()->from($this->getTable()) ->delete()->from($this->getTable())
->where("{$this->getKey()} = ?"); ->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; 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 protected function parseData(Define\Model $model, ?array $data, Implement\Repository\MapperParser $data_map): Define\Model
{ {
if ($data === null) { if ($data === null) {
@ -146,9 +158,9 @@ abstract class Repository implements Define\Repository
->update($this->getTable()) ->update($this->getTable())
->set($columns_string) ->set($columns_string)
->where("{$this->getKey()} = ?"); ->where("{$this->getKey()} = ?");
$values []= $model->{$this->getKey()}; $values []= $model->{$this->getIndex()};
$this->connection->execute($query, $values); $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 protected function fetchOne(string $query, ?array $data = null): Define\Model
{ {