diff --git a/src/Collection/HasManyCollection.php b/src/Collection/HasManyCollection.php index 0b54636a..36990ec0 100644 --- a/src/Collection/HasManyCollection.php +++ b/src/Collection/HasManyCollection.php @@ -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; } diff --git a/tests/cases/integration/Relationships/relationships.HasManyCollection.phpt b/tests/cases/integration/Relationships/relationships.HasManyCollection.phpt index a87b9da8..e89dd312 100644 --- a/tests/cases/integration/Relationships/relationships.HasManyCollection.phpt +++ b/tests/cases/integration/Relationships/relationships.HasManyCollection.phpt @@ -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);