Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nette db update #30

Merged
merged 7 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coding-standard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ jobs:
run: vendor/bin/phpstan analyse --level 5 src

- name: Run Coding Standard
run: vendor/modul-is/coding-standard/ecs check src --preset php81
run: vendor/modul-is/coding-standard/ecs check src --preset php82
436 changes: 230 additions & 206 deletions composer.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@ To delete a single row, use `delete()` with an entity.
$this->AnimalRepository->delete($animalEntity);
```

To delete multiple rows, use `removeCollection()` with an array or a collection.
To delete multiple rows, use `deleteCollection()` with an array or a collection.
```
$entityArray = [$zooEntity1, $zooEntity2];

$this->ZooRepository->removeCollection($entityArray);
$this->ZooRepository->deleteCollection($entityArray);
```

Alternatively we can delete a row only using its ID.
```
$this->ZooRepository->removeByID(1);
```
$this->ZooRepository->deleteByID(1);
```
18 changes: 7 additions & 11 deletions src/ModulIS/EntityCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,23 @@ class EntityCollection implements \Iterator, \Countable

public const DESC = 'DESC';

protected Selection $selection;

protected string|\Closure $entity;

protected string|null $refTable;

protected string|null $refColumn;

protected ?array $data;

private int|null $count;

private array $keys;


public function __construct(Selection $selection, $entity, $refTable = null, $refColumn = null)
public function __construct
(
protected Selection $selection,
$entity,
protected ?string $refTable = null,
protected ?string $refColumn = null
)
{
$this->selection = $selection;
$this->refTable = $refTable;
$this->refColumn = $refColumn;

try
{
\Nette\Utils\Callback::check($entity);
Expand Down
1 change: 0 additions & 1 deletion src/ModulIS/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ModulIS\Exception;


class InvalidArgumentException extends \InvalidArgumentException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ModulIS\Exception;


class InvalidPropertyDefinitionException extends InvalidStateException
{
}
1 change: 0 additions & 1 deletion src/ModulIS/Exception/InvalidStateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ModulIS\Exception;


class InvalidStateException extends \RuntimeException
{
}
1 change: 0 additions & 1 deletion src/ModulIS/Exception/MemberAccessException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ModulIS\Exception;


class MemberAccessException extends \LogicException
{
}
1 change: 0 additions & 1 deletion src/ModulIS/Exception/NotSupportedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace ModulIS\Exception;


class NotSupportedException extends \LogicException
{
}
9 changes: 4 additions & 5 deletions src/ModulIS/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
use Nette\Database\Table\ActiveRow;
use Nette\Database\Table\GroupedSelection;


class Record
{
private ?ActiveRow $row;

private array $values = [];

private array $modified = [];


final public function __construct(?ActiveRow $row = null)
final public function __construct
(
private ?ActiveRow $row = null
)
{
$this->row = $row;
}


Expand Down
13 changes: 5 additions & 8 deletions src/ModulIS/Reflection/EntityProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,12 @@ public function isOfExtraType(): bool

private static function getConvertedType(string $type): string
{
switch($type)
return match($type)
{
case 'array':
return 'string';
case 'bool':
return 'int';
default:
return $type;
}
'array' => 'string',
'bool' => 'int',
default => $type
};
}


Expand Down
58 changes: 29 additions & 29 deletions src/ModulIS/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@

abstract class Repository
{
protected Explorer $database;

protected string $table;

protected string $entity;

private Transaction $transaction;


public function __construct(Explorer $database)
public function __construct
(
protected Explorer $database
)
{
$this->database = $database;
$this->transaction = new Transaction($database->getConnection());

$ref = new \ReflectionClass($this);

if(!$this->table)
Expand Down Expand Up @@ -153,7 +149,7 @@ public function persist(Entity $entity): bool
}

$inserted = $this->getTable()
->insert($record->getModified());
->insert($record->getModified());

if(!$inserted instanceof IRow)
{
Expand All @@ -172,21 +168,7 @@ public function delete(Entity $entity): bool
$this->checkEntity($entity);
$record = $entity->toRecord();

if($record->hasRow())
{
return $this->transaction(fn() => $record->getRow()->delete() > 0);
}

return true;
}


/**
* @deprecated
*/
public function remove(Entity $entity): bool
{
return $this->delete($entity);
return $record->hasRow() ? $record->getRow()->delete() > 0 : true;
}


Expand All @@ -210,7 +192,7 @@ final protected function checkEntity(Entity $entity): void

final protected function transaction(\Closure $callback): mixed
{
return $this->transaction->transaction($callback);
return $this->database->getConnection()->transaction($callback);
}


Expand All @@ -230,9 +212,9 @@ private function isCollectionEmpty(array|EntityCollection|ArrayHash $collection)


/**
* Remove collection by transaction
* Delete collection by transaction
*/
public function removeCollection(array|EntityCollection|ArrayHash $collection): mixed
public function deleteCollection(array|EntityCollection|ArrayHash $collection): mixed
{
if($this->isCollectionEmpty($collection))
{
Expand All @@ -250,10 +232,28 @@ public function removeCollection(array|EntityCollection|ArrayHash $collection):


/**
* Remove single instance from database by ID
* Delete single instance from database by ID
*/
public function removeByID(int|string $id): bool
public function deleteByID(int|string $id): bool
{
return (bool) $this->getTable()->wherePrimary($id)->delete();
}


/**
* @deprecated
*/
public function removeCollection(array|EntityCollection|ArrayHash $collection): mixed
{
return $this->deleteCollection($collection);
}


/**
* @deprecated
*/
public function removeByID(int|string $id): bool
{
return $this->deleteByID($id);
}
}
85 changes: 0 additions & 85 deletions src/ModulIS/Transaction.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/cases/OrmTest/CollectionCaseTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CollectionCaseTest extends TestCase

Assert::same(2, $collection->count());

$animalRepository->removeCollection($collection);
$animalRepository->deleteCollection($collection);

$deleted = $animalRepository->findBy([]);

Expand Down
2 changes: 1 addition & 1 deletion tests/cases/OrmTest/RepositoryCaseTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class RepositoryCaseTest extends TestCase
/**
* TEST: Remove entity by ID
*/
$deletedByIdEntity = $repository->removeByID(1);
$deletedByIdEntity = $repository->deleteByID(1);

Assert::true($deletedByIdEntity);

Expand Down
Loading