diff --git a/src/Relationships/HasMany.php b/src/Relationships/HasMany.php index f379d434..5a7d53d8 100644 --- a/src/Relationships/HasMany.php +++ b/src/Relationships/HasMany.php @@ -157,6 +157,7 @@ public function add($entity): ?IEntity if (isset($this->toRemove[$entityHash])) { unset($this->toRemove[$entityHash]); + $this->tracked[$entityHash] = $entity; } else { $this->toAdd[$entityHash] = $entity; } diff --git a/tests/cases/integration/Relationships/relationships.manyHasMany.phpt b/tests/cases/integration/Relationships/relationships.manyHasMany.phpt index a7851255..b7ce1c84 100644 --- a/tests/cases/integration/Relationships/relationships.manyHasMany.phpt +++ b/tests/cases/integration/Relationships/relationships.manyHasMany.phpt @@ -312,7 +312,7 @@ class RelationshipManyHasManyTest extends DataTestCase $property->set([1, 2]); $book = $this->orm->books->getByIdChecked(1); - Assert::count(0, $book->tags->getEntitiesForPersistence()); + Assert::count(1, $book->tags->getEntitiesForPersistence()); } } diff --git a/tests/cases/integration/Relationships/relationships.oneHasMany.collection.phpt b/tests/cases/integration/Relationships/relationships.oneHasMany.collection.phpt index 5abdbbe2..f39a3285 100644 --- a/tests/cases/integration/Relationships/relationships.oneHasMany.collection.phpt +++ b/tests/cases/integration/Relationships/relationships.oneHasMany.collection.phpt @@ -422,6 +422,22 @@ class RelationshipsOneHasManyCollectionTest extends DataTestCase } + public function testReAdd(): void + { + $queries = $this->getQueries(function (): void { + Assert::count(0, $this->authorA->books->getEntitiesForPersistence()); + iterator_to_array($this->authorA->books); // SELECT ALL + Assert::count(2, $this->authorA->books->getEntitiesForPersistence()); + $this->authorA->books->set(iterator_to_array($this->authorA->books)); + Assert::count(2, $this->authorA->books->getEntitiesForPersistence()); + }); + + if ($queries !== null) { + Assert::count(1, $queries); + } + } + + private function createBook(): Book { static $id = 0;