From 4d2843c3685e9f35fe61b784fd62a4b512707cd2 Mon Sep 17 00:00:00 2001 From: lotyp Date: Fri, 3 May 2024 19:27:08 +0300 Subject: [PATCH] feat: add remove method --- composer.json | 2 +- psalm-baseline.xml | 1 + psalm.xml | 1 - src/ActiveRecord.php | 14 +++++++++++ {config => src/Bridge/Yii3}/yii-bootstrap.php | 0 tests/src/ActiveRecordTest.php | 24 +++++++++++++++++++ 6 files changed, 40 insertions(+), 2 deletions(-) rename {config => src/Bridge/Yii3}/yii-bootstrap.php (100%) diff --git a/composer.json b/composer.json index 09c7745..971a765 100644 --- a/composer.json +++ b/composer.json @@ -99,7 +99,7 @@ "indent-style": "space" }, "config-plugin": { - "bootstrap": "config/yii-bootstrap.php" + "bootstrap": "src/Bridge/Yii3/yii-bootstrap.php" }, "laravel": { "providers": [ diff --git a/psalm-baseline.xml b/psalm-baseline.xml index d34b240..44d7bad 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -6,6 +6,7 @@ + diff --git a/psalm.xml b/psalm.xml index 2cad805..760d5f9 100644 --- a/psalm.xml +++ b/psalm.xml @@ -16,7 +16,6 @@ - diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index f0084f5..9c272af 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -160,4 +160,18 @@ final public function deleteOrFail(bool $cascade = true): StateInterface return $entityManager->run(); } + + /** + * Prepares the current entity for deletion. + * + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + final public function remove(bool $cascade = true): EntityManagerInterface + { + /** @var EntityManager $entityManager */ + $entityManager = Facade::getEntityManager(); + + return $entityManager->delete($this, $cascade); + } } diff --git a/config/yii-bootstrap.php b/src/Bridge/Yii3/yii-bootstrap.php similarity index 100% rename from config/yii-bootstrap.php rename to src/Bridge/Yii3/yii-bootstrap.php diff --git a/tests/src/ActiveRecordTest.php b/tests/src/ActiveRecordTest.php index d199bc3..1d26a82 100644 --- a/tests/src/ActiveRecordTest.php +++ b/tests/src/ActiveRecordTest.php @@ -176,4 +176,28 @@ public function it_deletes_entity(): void $this::assertTrue($user->delete()->isSuccess()); $this::assertCount(1, User::findAll()); } + + /** + * @test + * + * @throws NotFoundExceptionInterface + * @throws ContainerExceptionInterface + * @throws Throwable + */ + #[Test] + public function it_deletes_multiple_entities_using_remove_method(): void + { + $userOne = User::find(1); + $userTwo = User::find(2); + + $userOne->remove(); + $userTwo->remove(); + + $this::assertCount(2, User::findAll()); + + $entityManager = Facade::getEntityManager(); + $this::assertTrue($entityManager->run()->isSuccess()); + + $this::assertCount(0, User::findAll()); + } }