Skip to content

Commit

Permalink
Merge pull request #255 from bowphp/papac-patch-1
Browse files Browse the repository at this point in the history
Update Database.php
  • Loading branch information
papac authored Aug 21, 2023
2 parents 0631ce6 + ff05d71 commit b397ce4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
14 changes: 11 additions & 3 deletions src/Database/Barry/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/Database/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -67,14 +73,14 @@ public function dropAll(): void
*/
public function __toString(): string
{
return json_encode($this->toArray());
return json_encode($this->all());
}

/**
* @inheritdoc
*/
public function jsonSerialize(): array
{
return $this->toArray();
return $this->all();
}
}
41 changes: 27 additions & 14 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static function connection(?string $name = null): ?Database
}

/**
* Get connexion name
* Get the connexion name
*
* @return string|null
*/
Expand Down Expand Up @@ -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
Expand All @@ -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(): void
{
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
*/
Expand Down Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/Query/DatabaseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit b397ce4

Please sign in to comment.