Skip to content

Commit

Permalink
collection: fix HasManyCollection::fetch() accesing invalid iterator …
Browse files Browse the repository at this point in the history
…state [closes #466]
  • Loading branch information
hrach committed Oct 9, 2020
1 parent 7272f30 commit 4e27857
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Collection/HasManyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ public function fetch(): ?IEntity
$this->fetchIterator = $this->getIterator();
}

if (($current = $this->fetchIterator->current()) !== null) {
if ($this->fetchIterator->valid()) {
$current = $this->fetchIterator->current();
$this->fetchIterator->next();
return $current;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ class RelationshipsHasManyCollectionTest extends DataTestCase
}


public function testGetBy(): void
{
$book = new Book();
$book->title = 'test';
$this->orm->books->attach($book);

$author = new Author();
$author->books->add($book);

Assert::null($author->books->toCollection()->getBy(['title' => 'another']));
Assert::same($book, $author->books->toCollection()->getBy(['title' => 'test']));
}


public function testFindByRemove(): void
{
$book = $this->orm->books->getByIdChecked(3);
Expand Down

0 comments on commit 4e27857

Please sign in to comment.