Skip to content

Commit f6a8cb9

Browse files
committed
HasMany: removeAll
Closes #276.
1 parent 0e0fab2 commit f6a8cb9

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

docs/relationships.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,12 @@ Also, you can use convenient methods to add, remove, and set entities in the rel
212212
$author->books->add($book);
213213
$author->books->remove($book);
214214
$author->books->set([$book]);
215+
$author->books->removeAll();
215216

216217
$book->tags->add($tag);
217218
$book->tags->remove($tag);
218219
$book->tags->set([$tag]);
220+
$book->tags->removeAll();
219221
```
220222

221223
The relationship property wrapper accepts both entity instances and an id (primary key value). If you pass an id, Orm will load the proper entity automatically. This behavior is available only if the entity is "attached" to the repository (fetched from storage, directly attached, or indirectly attached by another attached entity).

src/Relationships/HasMany.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,16 @@ public function has($entity): bool
214214
}
215215

216216

217+
public function removeAll(): bool
218+
{
219+
foreach ($this->getCollection() as $entity) {
220+
$this->remove($entity);
221+
}
222+
223+
return true;
224+
}
225+
226+
217227
public function set(array $data): bool
218228
{
219229
$wanted = [];

src/Relationships/IRelationshipCollection.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public function set(array $data): bool;
4242
public function remove($entity): ?IEntity;
4343

4444

45+
/**
46+
* Removes all entities.
47+
* @return bool
48+
*/
49+
public function removeAll(): bool;
50+
51+
4552
/**
4653
* @param E|string|int $entity
4754
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* @testCase
5+
* @dataProvider ../../../databases.ini
6+
*/
7+
8+
namespace NextrasTests\Orm\Integration\Relationships;
9+
10+
11+
use NextrasTests\Orm\Author;
12+
use NextrasTests\Orm\DataTestCase;
13+
use NextrasTests\Orm\Tag;
14+
use NextrasTests\Orm\TagFollower;
15+
use Tester\Assert;
16+
17+
18+
require_once __DIR__ . '/../../../bootstrap.php';
19+
20+
21+
class RelationshipOneHasManyRemoveAllTest extends DataTestCase
22+
{
23+
24+
public function testRemoveAllItems(): void
25+
{
26+
$author = new Author();
27+
$author->name = 'Stephen King';
28+
29+
$tagFollower = new TagFollower();
30+
$tagFollower->author = $author;
31+
$tagFollower->tag = new Tag('Horror');
32+
33+
$this->orm->authors->persistAndFlush($author);
34+
35+
Assert::same(1, $author->tagFollowers->count());
36+
$author->tagFollowers->removeAll();
37+
Assert::same(0, $author->tagFollowers->count());
38+
39+
$this->orm->authors->persistAndFlush($author);
40+
41+
Assert::same(0, $author->tagFollowers->count());
42+
Assert::same(0, $author->tagFollowers->countStored());
43+
}
44+
}
45+
46+
47+
$test = new RelationshipOneHasManyRemoveAllTest();
48+
$test->run();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
START TRANSACTION;
2+
INSERT INTO "public"."authors" ("name", "born_on", "web", "favorite_author_id") VALUES ('Stephen King', '2021-03-21'::date, 'http://www.example.com', NULL);
3+
SELECT CURRVAL('public.authors_id_seq');
4+
INSERT INTO "tags" ("name", "is_global") VALUES ('Horror', 'y');
5+
SELECT CURRVAL('public.tags_id_seq');
6+
INSERT INTO "tag_followers" ("created_at", "author_id", "tag_id") VALUES ('2021-12-02 19:21:00.000000'::timestamptz, 3, 4);
7+
COMMIT;
8+
SELECT "tag_followers".* FROM "tag_followers" AS "tag_followers" WHERE "tag_followers"."author_id" IN (3);
9+
SELECT "tag_followers".* FROM "tag_followers" AS "tag_followers" WHERE (NOT (("tag_followers"."author_id", "tag_followers"."tag_id") IN ((3, 4)))) AND ("tag_followers"."author_id" IN (3));
10+
START TRANSACTION;
11+
COMMIT;
12+
SELECT "tag_followers".* FROM "tag_followers" AS "tag_followers" WHERE "tag_followers"."author_id" IN (3);
13+
SELECT "author_id", COUNT(DISTINCT "tag_followers"."tag_id") as "count" FROM "tag_followers" AS "tag_followers" WHERE "tag_followers"."author_id" IN (3) GROUP BY "author_id";

0 commit comments

Comments
 (0)