From 627f513c4f6d5e7afd116cb5ebadde2832233703 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Mon, 21 Aug 2023 01:25:52 +0000 Subject: [PATCH 1/4] Update Database.php Add the transaction function --- src/Database/Database.php | 41 ++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 55a94dcc..7ed66eef 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -121,7 +121,7 @@ public static function connection(?string $name = null): ?Database } /** - * Get connexion name + * Get the connexion name * * @return string|null */ @@ -315,7 +315,7 @@ public static function delete(string $sql_statement, array $data = []): int } /** - * Load the query builder factory on table name + * Load the query builder factory on the table name * * @param string $table * @return QueryBuilder @@ -338,27 +338,17 @@ public static function table(string $table): QueryBuilder * @param callable $callback * @return void */ - public static function startTransaction(?callable $callback = null): void + public static function startTransaction(): mixed { static::verifyConnection(); if (!static::$adapter->getConnection()->inTransaction()) { static::$adapter->getConnection()->beginTransaction(); } - - if (is_callable($callback)) { - try { - call_user_func_array($callback, []); - - static::commit(); - } catch (DatabaseException $e) { - static::rollback(); - } - } } /** - * Check if database execution is in transaction + * Check if database execution is in the transaction * * @return bool */ @@ -389,6 +379,29 @@ public static function rollback(): void static::$adapter->getConnection()->rollBack(); } + /** + * Starting the start of a transaction wrapper on top of the callback + * + * @param callable $callback + * @return void + */ + public static function transaction(callable $callback): mixed + { + static::startTransaction(); + + try { + $result = call_user_func_array($callback, []); + + static::commit(); + + return $result; + } catch (DatabaseException $e) { + static::rollback(); + + throw $e; + } + } + /** * Starts the verification of the connection establishment * From 364ef3a6d8ad3e67528b3cdcd22633b453381225 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Mon, 21 Aug 2023 01:49:39 +0000 Subject: [PATCH 2/4] Fixes transform model collection to json --- src/Database/Barry/Model.php | 14 +++++++++++--- src/Database/Collection.php | 24 +++++++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/Database/Barry/Model.php b/src/Database/Barry/Model.php index 0e07a98a..9ce4c9ea 100644 --- a/src/Database/Barry/Model.php +++ b/src/Database/Barry/Model.php @@ -591,10 +591,10 @@ private function transtypeKeyValue(mixed $primary_key_value): mixed * Delete a record * * @param array $attributes - * @return int + * @return int|bool * @throws */ - public function update(array $attributes): int + public function update(array $attributes): int|bool { $primary_key_value = $this->getKeyValue(); @@ -820,7 +820,15 @@ public function getTable(): string */ public function toArray(): array { - return $this->attributes; + $data = []; + + foreach ($this->attributes as $key => $value) { + if (!in_array($key, $this->hidden)) { + $data[$key] = $value; + } + } + + return $data; } /** diff --git a/src/Database/Collection.php b/src/Database/Collection.php index 078f0f26..b91b61bd 100644 --- a/src/Database/Collection.php +++ b/src/Database/Collection.php @@ -47,7 +47,13 @@ public function toArray(): array */ public function toJson(int $option = 0): string { - return json_encode($this->toArray(), $option = 0); + $data = []; + + foreach ($this->toArray() as $model) { + $data[] = $model->toArray(); + } + + return json_encode($data, $option = 0); } /** @@ -67,7 +73,13 @@ public function dropAll(): void */ public function __toString(): string { - return json_encode($this->toArray()); + $data = []; + + foreach ($this->toArray() as $model) { + $data[] = $model->toArray(); + } + + return json_encode($data); } /** @@ -75,6 +87,12 @@ public function __toString(): string */ public function jsonSerialize(): array { - return $this->toArray(); + $data = []; + + foreach ($this->toArray() as $model) { + $data[] = $model->toArray(); + } + + return $data; } } From 85de991f8f171d8243c34aa2e0700231b3b2a433 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Mon, 21 Aug 2023 01:57:45 +0000 Subject: [PATCH 3/4] Fixes unity tests --- src/Database/Database.php | 2 +- tests/Database/Query/DatabaseQueryTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 7ed66eef..c5116625 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -338,7 +338,7 @@ public static function table(string $table): QueryBuilder * @param callable $callback * @return void */ - public static function startTransaction(): mixed + public static function startTransaction(): void { static::verifyConnection(); diff --git a/tests/Database/Query/DatabaseQueryTest.php b/tests/Database/Query/DatabaseQueryTest.php index 39fde5c1..1e42e41e 100644 --- a/tests/Database/Query/DatabaseQueryTest.php +++ b/tests/Database/Query/DatabaseQueryTest.php @@ -214,7 +214,7 @@ public function test_transaction_table(string $name) $database->insert("insert into pets values(:id, :name);", ["id" => 1, 'name' => 'Ploy']); $result = 0; - $database->startTransaction(function () use ($database, &$result) { + $database->transaction(function () use ($database, &$result) { $result = $database->delete("delete from pets where id = :id", ['id' => 1]); $this->assertEquals($database->inTransaction(), true); }); From ff05d714d78ae1e7d84a19e30f9564811660d100 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Mon, 21 Aug 2023 02:08:59 +0000 Subject: [PATCH 4/4] Fixes collection as json --- src/Database/Collection.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/Database/Collection.php b/src/Database/Collection.php index b91b61bd..01770f77 100644 --- a/src/Database/Collection.php +++ b/src/Database/Collection.php @@ -73,13 +73,7 @@ public function dropAll(): void */ public function __toString(): string { - $data = []; - - foreach ($this->toArray() as $model) { - $data[] = $model->toArray(); - } - - return json_encode($data); + return json_encode($this->all()); } /** @@ -87,12 +81,6 @@ public function __toString(): string */ public function jsonSerialize(): array { - $data = []; - - foreach ($this->toArray() as $model) { - $data[] = $model->toArray(); - } - - return $data; + return $this->all(); } }