Skip to content

Commit

Permalink
[DDC-2340] Fix bug with dirty collection matching + ordering.
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Mar 12, 2013
1 parent dba63c5 commit eca468b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
13 changes: 5 additions & 8 deletions lib/Doctrine/ORM/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,10 @@ public function __clone()
*/
public function matching(Criteria $criteria)
{
if ($this->isDirty) {
$this->initialize();
}

if ($this->initialized) {
return $this->coll->matching($criteria);
}
Expand All @@ -846,13 +850,6 @@ public function matching(Criteria $criteria)
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()
Expand All @@ -866,6 +863,6 @@ public function matching(Criteria $criteria)

$persister = $this->em->getUnitOfWork()->getEntityPersister($this->association['targetEntity']);

return new ArrayCollection(array_merge($persister->loadCriteria($criteria), $newObjects));
return new ArrayCollection($persister->loadCriteria($criteria));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,36 @@ public function testMatching()
$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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function testUnmappedSuperclassInHierarchy()
*/
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');
}
Expand Down

0 comments on commit eca468b

Please sign in to comment.