From 1cd06d5fe60c4708ca2e6a9d09c2fc6fb4bfcd50 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Sun, 23 May 2021 21:03:10 -0400 Subject: [PATCH 1/3] Add and Edit Model --- common/Alias/Model.php | 30 ++++++++++++++++++++++++++++++ common/Define/Model.php | 3 +++ 2 files changed, 33 insertions(+) diff --git a/common/Alias/Model.php b/common/Alias/Model.php index cead37d..ceec5e9 100644 --- a/common/Alias/Model.php +++ b/common/Alias/Model.php @@ -122,4 +122,34 @@ abstract class Model extends BaseModel implements ModelInterface { public function toArray(): array { return $this->asArray(); } + + protected static function parseInput($input): array { + return array_intersect_key((array) $input, array_combine(self::$fields, self::$fields)); + } + public static function add(ModelFactory $factory, $input): bool { + $data = self::parseInput($input); + $class = get_called_class(); + if (method_exists($class, 'find')) { + $obj = self::find($input); + } else { + $obj = $factory->find($class)->where($data)->one(); + } + if ($obj === false) { + $obj = $factory->create($class, $data); + return $obj->save(); + } + return false; + } + public function edit($input): bool { + $data = self::parseInput($input); + foreach (self::$fields as $field) { + if ($this->{$field} != $data[$field]) { + $this->{$field} = $data[$field]; + } + } + if ($this->isDirty()) { + return $this->save(); + } + return false; + } } diff --git a/common/Define/Model.php b/common/Define/Model.php index deab442..bce472d 100644 --- a/common/Define/Model.php +++ b/common/Define/Model.php @@ -11,4 +11,7 @@ interface Model { public function siblingOf(string $sibling_model_class, string $connecting_table, array $relation_definitions): ?array; public function toArray(): array; + + public static function add(ModelFactory $factory, $input): bool; + public function edit($input): bool; } From db2864395ca1c83af3cd09e113bb8e116117acd1 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Sun, 23 May 2021 21:07:48 -0400 Subject: [PATCH 2/3] FIX: Factory for find --- common/Alias/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/Alias/Model.php b/common/Alias/Model.php index ceec5e9..5dc310a 100644 --- a/common/Alias/Model.php +++ b/common/Alias/Model.php @@ -130,7 +130,7 @@ abstract class Model extends BaseModel implements ModelInterface { $data = self::parseInput($input); $class = get_called_class(); if (method_exists($class, 'find')) { - $obj = self::find($input); + $obj = self::find($factory, $input); } else { $obj = $factory->find($class)->where($data)->one(); } From 643c3e714f50e02c771c36ca8ba535ae0d3a8238 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Sun, 23 May 2021 21:27:13 -0400 Subject: [PATCH 3/3] FIX: Find where in add --- common/Alias/Model.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/common/Alias/Model.php b/common/Alias/Model.php index 5dc310a..4fed3ec 100644 --- a/common/Alias/Model.php +++ b/common/Alias/Model.php @@ -132,7 +132,11 @@ abstract class Model extends BaseModel implements ModelInterface { if (method_exists($class, 'find')) { $obj = self::find($factory, $input); } else { - $obj = $factory->find($class)->where($data)->one(); + $where = $data; + $where = array_values(array_walk($where, function(&$item, $key) { + $item = [$key, $item]; + })); + $obj = $factory->find($class)->where($where)->one(); } if ($obj === false) { $obj = $factory->create($class, $data);