From eca468b9d1a31e621fd32d1c4082dcb5db74701a Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Tue, 12 Mar 2013 22:48:56 +0100 Subject: [PATCH] [DDC-2340] Fix bug with dirty collection matching + ordering. --- lib/Doctrine/ORM/PersistentCollection.php | 13 ++++------ .../OneToManyBidirectionalAssociationTest.php | 26 +++++++++++++++++-- .../Mapping/BasicInheritanceMappingTest.php | 2 +- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/Doctrine/ORM/PersistentCollection.php b/lib/Doctrine/ORM/PersistentCollection.php index 33040d993..b644ec154 100644 --- a/lib/Doctrine/ORM/PersistentCollection.php +++ b/lib/Doctrine/ORM/PersistentCollection.php @@ -838,6 +838,10 @@ final class PersistentCollection implements Collection, Selectable */ public function matching(Criteria $criteria) { + if ($this->isDirty) { + $this->initialize(); + } + if ($this->initialized) { return $this->coll->matching($criteria); } @@ -846,13 +850,6 @@ final class PersistentCollection implements Collection, Selectable throw new \RuntimeException("Matching Criteria on PersistentCollection only works on OneToMany associations 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(); - } - $id = $this->em ->getClassMetadata(get_class($this->owner)) ->getSingleIdReflectionProperty() @@ -866,6 +863,6 @@ final class PersistentCollection implements Collection, Selectable $persister = $this->em->getUnitOfWork()->getEntityPersister($this->association['targetEntity']); - return new ArrayCollection(array_merge($persister->loadCriteria($criteria), $newObjects)); + return new ArrayCollection($persister->loadCriteria($criteria)); } } diff --git a/tests/Doctrine/Tests/ORM/Functional/OneToManyBidirectionalAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/OneToManyBidirectionalAssociationTest.php index 75d9d2abe..f513395d7 100644 --- a/tests/Doctrine/Tests/ORM/Functional/OneToManyBidirectionalAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/OneToManyBidirectionalAssociationTest.php @@ -173,14 +173,36 @@ class OneToManyBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctiona $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $results); $this->assertEquals(2, count($results)); } - + + /** + * @group DDC-2340 + */ + public function testMatchingOnDirtyCollection() + { + $this->_createFixture(); + + $product = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $this->product->getId()); + + $thirdFeature = new ECommerceFeature(); + $thirdFeature->setDescription('Model writing tutorial'); + + $features = $product->getFeatures(); + $features->add($thirdFeature); + + $results = $features->matching(new Criteria( + Criteria::expr()->eq('description', 'Model writing tutorial') + )); + + $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); diff --git a/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php b/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php index 2d493b0f6..c1e5a12f9 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php @@ -115,7 +115,7 @@ class BasicInheritanceMappingTest extends \Doctrine\Tests\OrmTestCase */ public function testUnmappedEntityInHierarchy() { - $this->setExpectedException('Doctrine\ORM\Mapping\MappingException', "Entity 'Doctrine\Tests\ORM\Mapping\HierarchyBEntity' has to be part of the discriminator map of 'Doctrine\Tests\ORM\Mapping\HierarchyBase' to be properly mapped in the inheritance hierarchy. Alternatively you can make 'Doctrine\Tests\ORM\Mapping\HierarchyBEntity' an abstract class to avoid this exception from occuring."); + $this->setExpectedException('Doctrine\ORM\Mapping\MappingException', "Entity 'Doctrine\Tests\ORM\Mapping\HierarchyBEntity' has to be part of the discriminator map of 'Doctrine\Tests\ORM\Mapping\HierarchyBase' to be properly mapped in the inheritance hierarchy. Alternatively you can make 'Doctrine\Tests\ORM\Mapping\HierarchyBEntity' an abstract class to avoid this exception from occurring."); $class = $this->_factory->getMetadataFor(__NAMESPACE__ . '\\HierarchyE'); }