From 51124218d5ab4af2a215420cb416126d9305f52f Mon Sep 17 00:00:00 2001 From: Aldarien Date: Fri, 9 Sep 2022 17:47:46 -0400 Subject: [PATCH 1/2] Check if value is set before serializing --- src/Alias/Model.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Alias/Model.php b/src/Alias/Model.php index 0555b70..406c064 100644 --- a/src/Alias/Model.php +++ b/src/Alias/Model.php @@ -79,6 +79,9 @@ abstract class Model implements ModelInterface continue; } $p = strtolower(str_replace('get', '', $method->getName())); + if (!isset($this->{$p})) { + continue; + } $output[$p] = $this->{$method->getName()}(); } return $output; From aff0d4333d843f7e7b8edcdc64d7c5fdbebd5dfa Mon Sep 17 00:00:00 2001 From: Aldarien Date: Mon, 12 Sep 2022 17:23:29 -0300 Subject: [PATCH 2/2] Added update to Repository --- src/Alias/Model/Repository.php | 53 ++++++++++++++++++++++++++++---- src/Concept/Model/Repository.php | 4 +++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/Alias/Model/Repository.php b/src/Alias/Model/Repository.php index ed9b008..014b545 100644 --- a/src/Alias/Model/Repository.php +++ b/src/Alias/Model/Repository.php @@ -131,6 +131,22 @@ abstract class Repository implements RepositoryInterface return $this->optional ?? []; } + public function getMethod(string $column, bool $get = true): string + { + $m = str_replace(' ', '', ucwords(str_replace('_', ' ', $column))); + if ($get) { + return "get{$m}"; + } + return "set{$m}"; + } + public function getProperty(string $method): string + { + $parts = preg_split('/(?=[A-Z])/', $method); + if (in_array(strtolower($parts[0]), ['get', 'set'])) { + array_shift($parts); + } + return strtolower(implode('_', $parts)); + } public function fillData(Model $model, array $data): Model { foreach ($this->getRequired() as $column) { @@ -145,6 +161,18 @@ abstract class Repository implements RepositoryInterface } return $model; } + public function mapArray(Model $model, array $data): array + { + foreach ($this->getColumns() as $column) { + $m = $this->getMethod($column); + $val = $model->{$m}(); + if (isset($data[$column])) { + continue; + } + $data[$column] = $val; + } + return $data; + } public function load(array $data): Model { return $this->fillData($this->getNewModel() @@ -155,16 +183,29 @@ abstract class Repository implements RepositoryInterface if (!$model->isDirty() and !$model->isNew()) { return; } - $cols = []; - $values = []; - foreach ($this->getColumns() as $column) { - $m = 'get' . ucwords($column); - $cols []= '?'; - $values []= $model->{$m}(); + if (!$model->isNew()) { + $this->update($model); + return; } + $values = $this->mapArray($model, []); + $cols = array_fill(0, count($values), '?'); $query = $this->getQueryBuilder()->insert($this->getTable())->columns($this->getColumns())->values($cols); $this->getConnection()->execute($query, $values); } + public function update(Model $model): void + { + if (!$model->isDirty() and !$model->isNew()) { + return; + } + $values = $this->mapArray($model, []); + $cols = array_map(function($column) { + return "{$column} = ?"; + }, $values); + $values = array_values($values); + $values []= $model->getId(); + $query = $this->getQueryBuilder()->update($this->getTable())->set($cols)->where(['id = ?']); + $this->getConnection()->execute($query, $values); + } public function create(array $data): Model { return $this->fillData($this->getNewModel() diff --git a/src/Concept/Model/Repository.php b/src/Concept/Model/Repository.php index c6b91d6..7a79fb9 100644 --- a/src/Concept/Model/Repository.php +++ b/src/Concept/Model/Repository.php @@ -28,9 +28,13 @@ interface Repository public function setOptional(array $columns): Repository; public function addOptional(string $column): Repository; public function getOptional(): array; + public function getMethod(string $column, bool $get = true): string; + public function getProperty(string $method): string; public function fillData(Model $model, array $data): Model; + public function mapArray(Model $model, array $data): array; public function load(array $data): Model; public function save(Model $model): void; + public function update(Model $model): void; public function create(array $data): Model; public function edit(Model $model, array $data): Model; public function delete(Model $model): void;