From 4b3ecfe67468085a8c30df3dc85444eff64bed9c Mon Sep 17 00:00:00 2001 From: Stefano Rodriguez Date: Fri, 7 Sep 2012 10:42:40 +0200 Subject: [PATCH 1/3] Added a failing test case on PersistentCollection::matching() when collection is not initialized and there are NEW entities in the collection --- .../OneToManyBidirectionalAssociationTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Functional/OneToManyBidirectionalAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/OneToManyBidirectionalAssociationTest.php index cea80fd6b..0efe76fdf 100644 --- a/tests/Doctrine/Tests/ORM/Functional/OneToManyBidirectionalAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/OneToManyBidirectionalAssociationTest.php @@ -173,6 +173,30 @@ class OneToManyBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctiona $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results); $this->assertEquals(2, count($results)); } + + public function testMatchingBis() + { + $this->_createFixture(); + + $product = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $this->product->getId()); + $features = $product->getFeatures(); + + $thirdFeature = new ECommerceFeature(); + $thirdFeature->setDescription('Third feature'); + $product->addFeature($thirdFeature); + + $results = $features->matching(new Criteria( + Criteria::expr()->eq('description', 'Third feature') + )); + + $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results); + $this->assertEquals(1, count($results)); + + $results = $features->matching(new Criteria()); + + $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results); + $this->assertEquals(3, count($results)); + } private function _createFixture() { From bb8dd6cb1168b8e3d18caed935365a1fe41044f4 Mon Sep 17 00:00:00 2001 From: Stefano Rodriguez Date: Fri, 7 Sep 2012 03:29:45 +0200 Subject: [PATCH 2/3] Fixes PersistentCollection::matching() when collection is not initialized and there are NEW entities in the collection --- lib/Doctrine/ORM/PersistentCollection.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/PersistentCollection.php b/lib/Doctrine/ORM/PersistentCollection.php index 30b0e01e7..ec973408d 100644 --- a/lib/Doctrine/ORM/PersistentCollection.php +++ b/lib/Doctrine/ORM/PersistentCollection.php @@ -42,6 +42,7 @@ use Closure; * @author Konsta Vesterinen * @author Roman Borschel * @author Giorgio Sironi + * @author Stefano Rodriguez * @todo Design for inheritance to allow custom implementations? */ final class PersistentCollection implements Collection, Selectable @@ -812,6 +813,13 @@ final class PersistentCollection implements Collection, Selectable throw new \RuntimeException("Matching Criteria on PersistentCollection only works on OneToMany assocations at the moment."); } + // If there are NEW objects we have to check if any of them matches the criteria + $newObjects = array(); + + if ($this->isDirty) { + $newObjects = $this->coll->matching($criteria)->toArray(); + } + $targetClass = $this->em->getClassMetadata(get_class($this->owner)); $id = $targetClass->getSingleIdReflectionProperty()->getValue($this->owner); @@ -824,7 +832,7 @@ final class PersistentCollection implements Collection, Selectable $persister = $this->em->getUnitOfWork()->getEntityPersister($this->association['targetEntity']); - return new ArrayCollection($persister->loadCriteria($criteria)); + return new ArrayCollection(array_merge($persister->loadCriteria($criteria), $newObjects)); } } From 9a14b404957f11ecdf3e5dc4a96b938847d45618 Mon Sep 17 00:00:00 2001 From: Stefano Rodriguez Date: Fri, 7 Sep 2012 14:44:18 +0200 Subject: [PATCH 3/3] use of assertCount --- .../Functional/OneToManyBidirectionalAssociationTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/OneToManyBidirectionalAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/OneToManyBidirectionalAssociationTest.php index 0efe76fdf..75d9d2abe 100644 --- a/tests/Doctrine/Tests/ORM/Functional/OneToManyBidirectionalAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/OneToManyBidirectionalAssociationTest.php @@ -174,7 +174,7 @@ class OneToManyBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctiona $this->assertEquals(2, count($results)); } - public function testMatchingBis() + public function testMatchingBis() { $this->_createFixture(); @@ -190,12 +190,12 @@ class OneToManyBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctiona )); $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results); - $this->assertEquals(1, count($results)); + $this->assertCount(1, $results); $results = $features->matching(new Criteria()); $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results); - $this->assertEquals(3, count($results)); + $this->assertCount(3, $results); } private function _createFixture()