Skip to content

Commit

Permalink
relationship: reading removed entity retracks it (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach authored Apr 4, 2021
1 parent 97e9e89 commit 2e2cdb7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Relationships/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public function add($entity): ?IEntity

if (isset($this->toRemove[$entityHash])) {
unset($this->toRemove[$entityHash]);
$this->tracked[$entityHash] = $entity;
} else {
$this->toAdd[$entityHash] = $entity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2e2cdb7

Please sign in to comment.