Commit 49a24c4e authored by romanb's avatar romanb

[2.0] Fixed #2478.

parent d30e8143
...@@ -177,9 +177,7 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect ...@@ -177,9 +177,7 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect
*/ */
public function hydrateAdd($element) public function hydrateAdd($element)
{ {
if ( ! $this->contains($element)) { $this->_coll->add($element);
$this->_coll->add($element);
}
// If _backRefFieldName is set, then the association is bidirectional // If _backRefFieldName is set, then the association is bidirectional
// and we need to set the back reference. // and we need to set the back reference.
...@@ -206,9 +204,7 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect ...@@ -206,9 +204,7 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect
*/ */
public function hydrateSet($key, $element) public function hydrateSet($key, $element)
{ {
if ( ! $this->contains($element)) { $this->_coll->set($key, $element);
$this->_coll->set($key, $element);
}
// If _backRefFieldName is set, then the association is bidirectional // If _backRefFieldName is set, then the association is bidirectional
// and we need to set the back reference. // and we need to set the back reference.
......
...@@ -451,13 +451,10 @@ class UnitOfWork implements PropertyChangedListener ...@@ -451,13 +451,10 @@ class UnitOfWork implements PropertyChangedListener
$actualData[$name] = $refProp->getValue($entity); $actualData[$name] = $refProp->getValue($entity);
} }
if ($class->isCollectionValuedAssociation($name) && $actualData[$name] !== null) { if ($class->isCollectionValuedAssociation($name) && $actualData[$name] !== null
&& ! ($actualData[$name] instanceof PersistentCollection)) {
// If $actualData[$name] is Collection then unwrap the array // If $actualData[$name] is Collection then unwrap the array
if ( ! $actualData[$name] instanceof ArrayCollection) { if ( ! $actualData[$name] instanceof ArrayCollection) {
if ($actualData[$name] instanceof PersistentCollection) {
$actualData[$name] = $actualData[$name]->toArray();
}
$actualData[$name] = new ArrayCollection($actualData[$name]); $actualData[$name] = new ArrayCollection($actualData[$name]);
} }
...@@ -1136,8 +1133,9 @@ class UnitOfWork implements PropertyChangedListener ...@@ -1136,8 +1133,9 @@ class UnitOfWork implements PropertyChangedListener
$visited[$oid] = $entity; // Mark visited $visited[$oid] = $entity; // Mark visited
$class = $this->_em->getClassMetadata(get_class($entity)); $class = $this->_em->getClassMetadata(get_class($entity));
$entityState = $this->getEntityState($entity, self::STATE_NEW); $entityState = $this->getEntityState($entity, self::STATE_NEW);
switch ($entityState) { switch ($entityState) {
case self::STATE_MANAGED: case self::STATE_MANAGED:
// Nothing to do, except if policy is "deferred explicit" // Nothing to do, except if policy is "deferred explicit"
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
namespace Doctrine\Tests\ORM\Functional; namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\Models\ECommerce\ECommerceCart, use Doctrine\Tests\Models\ECommerce\ECommerceCart,
Doctrine\Tests\Models\ECommerce\ECommerceCategory, Doctrine\Tests\Models\ECommerce\ECommerceFeature,
Doctrine\Tests\Models\ECommerce\ECommerceCustomer, Doctrine\Tests\Models\ECommerce\ECommerceCustomer,
Doctrine\Tests\Models\ECommerce\ECommerceProduct; Doctrine\Tests\Models\ECommerce\ECommerceProduct;
...@@ -48,31 +48,40 @@ class StandardEntityPersisterTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -48,31 +48,40 @@ class StandardEntityPersisterTest extends \Doctrine\Tests\OrmFunctionalTestCase
*/ */
public function testAddPersistRetrieve() public function testAddPersistRetrieve()
{ {
$category = new ECommerceCategory(); $f1 = new ECommerceFeature;
$category->setName('Eletronics'); $f1->setDescription('AC-3');
$product = new ECommerceProduct(); $f2 = new ECommerceFeature;
$product->setName('MP3 Player Foo'); $f2->setDescription('DTS');
$category->addProduct($product);
$p = new ECommerceProduct;
$product2 = new ECommerceProduct(); $p->addFeature($f1);
$product2->setName('MP3 Player Bar'); $p->addfeature($f2);
$category->addProduct($product2); $this->_em->persist($p);
$this->_em->persist($category);
$this->_em->flush(); $this->_em->flush();
// He reported that using $this->_em->clear(); after flush fixes the problem. $this->assertEquals(2, count($p->getFeatures()));
// It should work out of the box. That's what we are testing. $this->assertTrue($p->getFeatures() instanceof \Doctrine\ORM\PersistentCollection);
$q = $this->_em->createQuery(
'SELECT p, f
FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p
JOIN p.features f'
);
$q = $this->_em->createQuery('
SELECT c, p
FROM Doctrine\Tests\Models\ECommerce\ECommerceCategory c
LEFT JOIN c.products p
');
$res = $q->getResult(); $res = $q->getResult();
$this->assertEquals(1, count($res)); $this->assertEquals(2, count($p->getFeatures()));
$this->assertEquals(2, count($res[0]->getProducts())); $this->assertTrue($p->getFeatures() instanceof \Doctrine\ORM\PersistentCollection);
// Check that the features are the same instances still
foreach ($p->getFeatures() as $feature) {
if ($feature->getDescription() == 'AC-3') {
$this->assertTrue($feature === $f1);
} else {
$this->assertTrue($feature === $f2);
}
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment