diff --git a/src/Relationships/ManyHasMany.php b/src/Relationships/ManyHasMany.php index d775f5d4..45d542e7 100644 --- a/src/Relationships/ManyHasMany.php +++ b/src/Relationships/ManyHasMany.php @@ -12,7 +12,6 @@ use Nextras\Orm\Entity\IEntity; use Traversable; use function assert; -use function spl_object_hash; class ManyHasMany extends HasMany @@ -92,11 +91,11 @@ protected function updateRelationshipAdd(IEntity $entity): void return; } + $this->updatingReverseRelationship = true; $otherSide = $entity->getProperty($this->metadataRelationship->property); assert($otherSide instanceof ManyHasMany); - $otherSide->collection = null; - $otherSide->toAdd[spl_object_hash($this->parent)] = $this->parent; - $otherSide->modify(); + $otherSide->add($this->parent); + $this->updatingReverseRelationship = false; } @@ -106,12 +105,10 @@ protected function updateRelationshipRemove(IEntity $entity): void return; } + $this->updatingReverseRelationship = true; $otherSide = $entity->getProperty($this->metadataRelationship->property); assert($otherSide instanceof ManyHasMany); - $otherSide->collection = null; - $entityHash = spl_object_hash($this->parent); - $otherSide->toRemove[$entityHash] = $this->parent; - unset($otherSide->tracked[$entityHash]); - $otherSide->modify(); + $otherSide->remove($this->parent); + $this->updatingReverseRelationship = false; } } diff --git a/tests/cases/integration/Relationships/relationships.manyHasMany.phpt b/tests/cases/integration/Relationships/relationships.manyHasMany.phpt index c45452dc..9510d88d 100644 --- a/tests/cases/integration/Relationships/relationships.manyHasMany.phpt +++ b/tests/cases/integration/Relationships/relationships.manyHasMany.phpt @@ -292,6 +292,16 @@ class RelationshipManyHasManyTest extends DataTestCase ]); Assert::same(1, $books->countStored()); } + + + public function testSymmetricRelationship() + { + $tag = $this->orm->tags->getById(2); + $tag->books->set([1, 2]); // no change + + $book = $this->orm->books->getById(1); + Assert::count(0, $book->tags->getEntitiesForPersistence()); + } }