Commit a0fc0985 authored by beberlei's avatar beberlei

[2.0] DDC-353 - Fix UnitOfWork::createEntity and StandardPersister::refresh()...

[2.0] DDC-353 - Fix UnitOfWork::createEntity and StandardPersister::refresh() not marking created Proxy instances as MANAGED like EntityManager::getReference() does
parent 536aca23
...@@ -489,10 +489,11 @@ class StandardEntityPersister ...@@ -489,10 +489,11 @@ class StandardEntityPersister
} }
$newData[$field] = $found; $newData[$field] = $found;
} else { } else {
// FIXME: What is happening with subClassees here?
$proxy = $this->_em->getProxyFactory()->getProxy($assoc->targetEntityName, $joinColumnValues); $proxy = $this->_em->getProxyFactory()->getProxy($assoc->targetEntityName, $joinColumnValues);
$this->_class->reflFields[$field]->setValue($entity, $proxy); $this->_class->reflFields[$field]->setValue($entity, $proxy);
$newData[$field] = $proxy; $newData[$field] = $proxy;
$this->_em->getUnitOfWork()->addToIdentityMap($proxy); $this->_em->getUnitOfWork()->registerManaged($proxy, $joinColumnValues, array());
} }
} }
} else { } else {
......
...@@ -1808,8 +1808,7 @@ class UnitOfWork implements PropertyChangedListener ...@@ -1808,8 +1808,7 @@ class UnitOfWork implements PropertyChangedListener
$newValue = $assoc->load($entity, null, $this->_em, $associatedId); $newValue = $assoc->load($entity, null, $this->_em, $associatedId);
} else { } else {
$newValue = $this->_em->getProxyFactory()->getProxy($assoc->targetEntityName, $associatedId); $newValue = $this->_em->getProxyFactory()->getProxy($assoc->targetEntityName, $associatedId);
$this->_entityIdentifiers[spl_object_hash($newValue)] = $associatedId; $this->registerManaged($newValue, $associatedId, array());
$this->_identityMap[$targetClass->rootEntityName][$relatedIdHash] = $newValue;
} }
} }
$this->_originalEntityData[$oid][$field] = $newValue; $this->_originalEntityData[$oid][$field] = $newValue;
......
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\ORM\UnitOfWork;
require_once __DIR__ . '/../../../TestInit.php';
class DDC353Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
try {
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC353File'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\DDC353Picture'),
));
} catch(\Exception $e) {
}
}
public function testWorkingCase()
{
$file = new DDC353File;
$picture = new DDC353Picture;
$picture->setFile($file);
$em = $this->_em;
$em->persist($picture);
$em->flush();
$em->clear();
$fileId = $file->getFileId();
$this->assertTrue($fileId > 0);
$file = $em->getReference('Doctrine\Tests\ORM\Functional\Ticket\DDC353File', $fileId);
$this->assertEquals(UnitOfWork::STATE_MANAGED, $em->getUnitOfWork()->getEntityState($file), "Reference Proxy should be marked MANAGED.");
$picture = $em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC353Picture', $picture->getPictureId());
$this->assertEquals(UnitOfWork::STATE_MANAGED, $em->getUnitOfWork()->getEntityState($picture->getFile()), "Lazy Proxy should be marked MANAGED.");
$em->remove($picture);
$em->flush();
}
public function testFailingCase()
{
$file = new DDC353File;
$picture = new DDC353Picture;
$picture->setFile($file);
$em = $this->_em;
$em->persist($picture);
$em->flush();
$em->clear();
$fileId = $file->getFileId();
$pictureId = $picture->getPictureId();
$this->assertTrue($fileId > 0);
$picture = $em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC353Picture', $pictureId);
$this->assertEquals(UnitOfWork::STATE_MANAGED, $em->getUnitOfWork()->getEntityState($picture->getFile()), "Lazy Proxy should be marked MANAGED.");
$em->remove($picture);
$em->flush();
}
}
/**
* @Entity
*/
class DDC353Picture
{
/**
* @Column(name="picture_id", type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
private $pictureId;
/**
* @ManyToOne(targetEntity="DDC353File", cascade={"persist", "remove"})
* @JoinColumns({
* @JoinColumn(name="file_id", referencedColumnName="file_id")
* })
*/
private $file;
/**
* Get pictureId
*/
public function getPictureId()
{
return $this->pictureId;
}
/**
* Set product
*/
public function setProduct($value)
{
$this->product = $value;
}
/**
* Get product
*/
public function getProduct()
{
return $this->product;
}
/**
* Set file
*/
public function setFile($value)
{
$this->file = $value;
}
/**
* Get file
*/
public function getFile()
{
return $this->file;
}
}
/**
* @Entity
*/
class DDC353File
{
/**
* @Column(name="file_id", type="integer")
* @Id
* @GeneratedValue(strategy="AUTO")
*/
public $fileId;
/**
* Get fileId
*/
public function getFileId()
{
return $this->fileId;
}
}
...@@ -123,13 +123,15 @@ class ObjectHydratorTest extends HydrationTestCase ...@@ -123,13 +123,15 @@ class ObjectHydratorTest extends HydrationTestCase
) )
); );
$proxyInstance = new \Doctrine\Tests\Models\ECommerce\ECommerceShipping();
// mocking the proxy factory // mocking the proxy factory
$proxyFactory = $this->getMock('Doctrine\ORM\Proxy\ProxyFactory', array('getProxy'), array(), '', false, false, false); $proxyFactory = $this->getMock('Doctrine\ORM\Proxy\ProxyFactory', array('getProxy'), array(), '', false, false, false);
$proxyFactory->expects($this->once()) $proxyFactory->expects($this->once())
->method('getProxy') ->method('getProxy')
->with($this->equalTo('Doctrine\Tests\Models\ECommerce\ECommerceShipping'), ->with($this->equalTo('Doctrine\Tests\Models\ECommerce\ECommerceShipping'),
array('id' => 42)) array('id' => 42))
->will($this->returnValue(new \stdClass)); ->will($this->returnValue($proxyInstance));
$this->_em->setProxyFactory($proxyFactory); $this->_em->setProxyFactory($proxyFactory);
......
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