From f0be25b6d1dadf4328410e3366c732e876fa769e Mon Sep 17 00:00:00 2001 From: scrs_zdenek Date: Tue, 27 Aug 2024 13:48:18 +0200 Subject: [PATCH] dev --- readme.md | 7 +------ src/Drago/Database/Database.php | 18 ++++++++++-------- tests/Database/Database.phpt | 31 ++++++++++++++++++++++--------- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/readme.md b/readme.md index 838b219..a6398a1 100644 --- a/readme.md +++ b/readme.md @@ -33,12 +33,7 @@ class Model Reading records from table. ```php -$this->model->read(); -``` - -Reading records from specific columns. -```php -$this->model->read('column'); +$this->model->read('*'); ``` Find records by column name. diff --git a/src/Drago/Database/Database.php b/src/Drago/Database/Database.php index 95651c3..1fb4882 100644 --- a/src/Drago/Database/Database.php +++ b/src/Drago/Database/Database.php @@ -52,9 +52,9 @@ public function command(): ExtraFluent */ public function read(...$args): ExtraFluent { - $command = $this->command(); - $command = $args ? $command->select(...$args) : $command->select('*'); - return $command->from($this->getTableName()); + return $this->command() + ->select(...$args) + ->from($this->getTableName()); } @@ -65,7 +65,7 @@ public function read(...$args): ExtraFluent */ public function find(string $column, int|string $args): ExtraFluent { - return $this->read() + return $this->read('*') ->where('%n = ?', $column, $args); } @@ -77,7 +77,7 @@ public function find(string $column, int|string $args): ExtraFluent */ public function get(int $id): ExtraFluent { - return $this->read() + return $this->read('*') ->where('%n = ?', $this->getPrimaryKey(), $id); } @@ -88,7 +88,8 @@ public function get(int $id): ExtraFluent */ public function delete(string $column, int|string $args): ExtraFluent { - return $this->command()->delete() + return $this->command() + ->delete() ->from($this->getTableName()) ->where('%n = ?', $column, $args); } @@ -102,6 +103,7 @@ public function delete(string $column, int|string $args): ExtraFluent public function save(mixed $values): Result|int|null { $key = $this->getPrimaryKey(); + $table = $this->getTableName(); if ($values instanceof Entity) { $values = $values->toArray(); @@ -112,8 +114,8 @@ public function save(mixed $values): Result|int|null $id = $values[$key] ?? null; $query = $id > 0 - ? $this->getConnection()->update($this->getTableName(), $values)->where('%n = ?', $key, $id) - : $this->getConnection()->insert($this->getTableName(), $values); + ? $this->getConnection()->update($table, $values)->where('%n = ?', $key, $id) + : $this->getConnection()->insert($table, $values); return $query->execute(); } diff --git a/tests/Database/Database.phpt b/tests/Database/Database.phpt index 73a26b3..a145238 100644 --- a/tests/Database/Database.phpt +++ b/tests/Database/Database.phpt @@ -20,7 +20,9 @@ function database(): TestDatabase test('Get record by id', function () { - $row = database()->get(1)->record(); + $row = database() + ->get(1) + ->record(); Assert::same(1, $row->id); Assert::same('Hello', $row->sample); @@ -28,7 +30,9 @@ test('Get record by id', function () { test('Find all records', function () { - $row = database()->read()->recordAll(); + $row = database() + ->read('*') + ->recordAll(); Assert::type('array', $row); }); @@ -46,7 +50,9 @@ test('Insert a record with an entity', function () { $entity->sample = 'Insert'; database()->save($entity); - $row = database()->get(2)->record(); + $row = database() + ->get(2) + ->record(); Assert::same(2, $row->id); Assert::same('Insert', $row->sample); @@ -54,9 +60,11 @@ test('Insert a record with an entity', function () { test('Update the record with the entity', function () { - $row = database()->get(2)->record(); - $row->sample = 'Update'; + $row = database() + ->get(2) + ->record(); + $row->sample = 'Update'; database()->save($row); Assert::same(2, $row->id); @@ -65,21 +73,24 @@ test('Update the record with the entity', function () { test('Get table name', function () { - $table = database()->getTableName(); + $table = database() + ->getTableName(); Assert::same('test', $table); }); test('Get table column primary key', function () { - $primaryKey = database()->getPrimaryKey(); + $primaryKey = database() + ->getPrimaryKey(); Assert::same('id', $primaryKey); }); test('Get class name', function () { - $className = database()->getClassName(); + $className = database() + ->getClassName(); Assert::same(TestEntity::class, $className); }); @@ -87,7 +98,9 @@ test('Get class name', function () { test('Delete record by id', function () { database()->delete('id', 2)->execute(); - $row = database()->get(2)->record(); + $row = database() + ->get(2) + ->record(); Assert::null($row); });