From 0938066d698680a0a726321e2a7082818fc5af7d Mon Sep 17 00:00:00 2001 From: "Don Wilson (local webserver)" Date: Fri, 13 Oct 2023 18:23:40 -0500 Subject: [PATCH] bug fix for model find method --- src/Magnetar/Database/QueryBuilder.php | 14 +++----- .../Model/Exceptions/ModelException.php | 13 +++++++ .../Exceptions/ModelNotFoundException.php | 13 +++++++ src/Magnetar/Model/HasLookupTrait.php | 15 +++++--- src/Magnetar/Model/Model.php | 36 +++++++++++-------- src/Magnetar/Utilities/Internals.php | 29 +++++++++++++++ src/Magnetar/Utilities/Str.php | 22 ++++++++++++ 7 files changed, 114 insertions(+), 28 deletions(-) create mode 100644 src/Magnetar/Model/Exceptions/ModelException.php create mode 100644 src/Magnetar/Model/Exceptions/ModelNotFoundException.php create mode 100644 src/Magnetar/Utilities/Internals.php diff --git a/src/Magnetar/Database/QueryBuilder.php b/src/Magnetar/Database/QueryBuilder.php index 79fbe3f..d1e91b4 100644 --- a/src/Magnetar/Database/QueryBuilder.php +++ b/src/Magnetar/Database/QueryBuilder.php @@ -378,8 +378,6 @@ protected function buildQueryAndParams(): array { /** * Build the query and fetch the results * @return array - * - * @todo split off into a separate QueryBuilder, implement fetch() and fetchOne() using adapter */ public function fetch(): array { [$query, $params] = $this->buildQueryAndParams(); @@ -393,11 +391,9 @@ public function fetch(): array { /** * Build the query and fetch a single row - * @return array - * - * @todo split off into a separate QueryBuilder, implement fetch() and fetchOne() using adapter + * @return array|false */ - public function fetchOne(): array { + public function fetchOne(): array|false { [$query, $params] = $this->buildQueryAndParams(); // reset query builder (to prevent accidental reuse) @@ -410,11 +406,9 @@ public function fetchOne(): array { /** * Build the query and fetch a column as a simple array * @param string|int $column_key The column to use as the array key for the results. If empty, fetches the first column - * @return array - * - * @todo split off into a separate QueryBuilder, implement fetch() and fetchOne() and fetchCol() using adapter + * @return array|false */ - public function fetchCol(string|int $column_key=0): array { + public function fetchCol(string|int $column_key=0): array|false { [$query, $params] = $this->buildQueryAndParams(); // reset query builder (to prevent accidental reuse) diff --git a/src/Magnetar/Model/Exceptions/ModelException.php b/src/Magnetar/Model/Exceptions/ModelException.php new file mode 100644 index 0000000..da916e9 --- /dev/null +++ b/src/Magnetar/Model/Exceptions/ModelException.php @@ -0,0 +1,13 @@ +_data = DB::connection($this->connection_name) + $data = DB::connection($this->connection_name) ->table($this->table) ->where($this->identifier, $id) ->fetchOne(); - return $this; + if(false === $data) { + throw new Exceptions\ModelNotFoundException('Model not found in table ['. $this->table .'] with identifier ['. $id .']'); + } + + return new static($data); } } \ No newline at end of file diff --git a/src/Magnetar/Model/Model.php b/src/Magnetar/Model/Model.php index c1eb9de..27e9857 100644 --- a/src/Magnetar/Model/Model.php +++ b/src/Magnetar/Model/Model.php @@ -8,6 +8,8 @@ use Magnetar\Model\HasDirtyTrait; use Magnetar\Model\HasLookupTrait; use Magnetar\Model\HasMutableTrait; + use Magnetar\Utilities\Str; + use Magnetar\Utilities\Internals; /** * Model class for interacting with the database. @@ -44,18 +46,21 @@ class Model implements ArrayAccess { protected string $identifier = 'id'; /** - * AbstractObject constructor - * @param int|null $id The ID of the object to pull + * Constructor + * @param array|string|int|null $data The ID of the model to pull */ public function __construct( - int|null $id=null + array|string|int|null $data=null ) { // determine table (if not set) $this->_determineModelTable(); - if(null !== $id) { + if(is_array($data)) { + // prefill model data + $this->_data = $data; + } elseif(is_string($data) || is_int($data)) { // pull model data - $this->find($id); + $this->find($data); } } @@ -69,15 +74,8 @@ protected function _determineModelTable(): void { return; } - // get the class name - $class = get_class($this); - - // get the table name - $parts = explode('\\', $class); - $class_name = end($parts); - - // convert to snake_case - $this->table = strtolower(preg_replace('/(?table = Str::snake_case(Internals::class_basename_instance($this)); } /** @@ -150,4 +148,14 @@ public function __set(string $key, mixed $value): void { public function __isset(string $key): bool { return isset($this->_data[ $key ]); } + + /** + * Handle dynamic static method calls + * @param mixed $method The method to call + * @param mixed $arguments The arguments to pass to the method + * @return mixed + */ + public static function __callStatic(mixed $method, mixed $arguments): mixed { + return (new static)->$method(...$arguments); + } } \ No newline at end of file diff --git a/src/Magnetar/Utilities/Internals.php b/src/Magnetar/Utilities/Internals.php new file mode 100644 index 0000000..f7fd256 --- /dev/null +++ b/src/Magnetar/Utilities/Internals.php @@ -0,0 +1,29 @@ +