From 8f98094398276873b2ffc409099786e3ffec003b Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Sat, 28 Mar 2020 13:23:28 +0100 Subject: [PATCH] relationships: fixed invalidating cache after persist & before flush [closes #386] --- src/Relationships/OneHasMany.php | 1 + .../relationships.oneHasMany.phpt | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/Relationships/OneHasMany.php b/src/Relationships/OneHasMany.php index 5cd4cd5d..459d1534 100644 --- a/src/Relationships/OneHasMany.php +++ b/src/Relationships/OneHasMany.php @@ -42,6 +42,7 @@ public function doPersist() $this->isModified = false; $this->collection = null; $this->getRelationshipMapper()->clearCache(); + $this->relationshipMapper = null; } diff --git a/tests/cases/integration/Relationships/relationships.oneHasMany.phpt b/tests/cases/integration/Relationships/relationships.oneHasMany.phpt index 41ce8aab..0d919b76 100644 --- a/tests/cases/integration/Relationships/relationships.oneHasMany.phpt +++ b/tests/cases/integration/Relationships/relationships.oneHasMany.phpt @@ -14,6 +14,7 @@ use NextrasTests\Orm\Author; use NextrasTests\Orm\Book; use NextrasTests\Orm\DataTestCase; use NextrasTests\Orm\Helper; +use NextrasTests\Orm\Publisher; use NextrasTests\Orm\TagFollower; use Tester\Assert; use Tester\Environment; @@ -238,6 +239,51 @@ class RelationshipOneHasManyTest extends DataTestCase sort($ids); Assert::same([1, 2], $ids); } + + + public function testCountAfterRemoveAndFlushAndCount() + { + $author = new Author(); + $author->name = 'The Imp'; + $author->web = 'localhost'; + $author->born = '2000-01-01 12:12:12'; + + $this->orm->authors->attach($author); + + $publisher = new Publisher(); + $publisher->name = 'Valyria'; + + $book = new Book(); + $book->author = $author; + $book->title = 'The Wall'; + $book->publisher = $publisher; + $book->translator = $author; + + $this->orm->authors->persistAndFlush($author); + + Assert::same(1, \count($author->books)); + + foreach ($author->books as $book) { + $this->orm->books->remove($book); + } + + Assert::same(0, \count($author->books)); + + $this->orm->books->flush(); + + Assert::same(0, \count($author->books)); + + $book3 = new Book(); + $book3->author = $author; + $book3->title = 'The Wall III'; + $book3->publisher = $publisher; + + Assert::same(1, \count($author->books)); + + $this->orm->books->persist($book3); + + Assert::same(1, \count($author->books)); + } }