Skip to content

Commit

Permalink
relationships/many has many: fixed updating reverse side of relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed May 8, 2020
1 parent dd9d092 commit d495ee1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/Relationships/ManyHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Nextras\Orm\Entity\IEntity;
use Traversable;
use function assert;
use function spl_object_hash;


class ManyHasMany extends HasMany
Expand Down Expand Up @@ -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;
}


Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}


Expand Down

0 comments on commit d495ee1

Please sign in to comment.