1
0
mirror of synced 2025-01-29 19:41:45 +03:00

DDC-1448 - Add support for ObjectManagerAware interface and PersistentObject in ORM

This commit is contained in:
Benjamin Eberlei 2011-11-19 12:41:06 +01:00
parent 8eaf160ead
commit bda593a66d
4 changed files with 145 additions and 4 deletions

View File

@ -1993,4 +1993,22 @@ class ClassMetadataInfo implements ClassMetadata
{
return isset($assoc['joinTable']['quoted']) ? $platform->quoteIdentifier($assoc['joinTable']['name']) : $assoc['joinTable']['name'];
}
/**
* @param string $fieldName
* @return bool
*/
public function isAssociationInverseSide($fieldName)
{
return isset($this->associationMappings[$fieldName]) && ! $this->associationMappings[$fieldName]['isOwningSide'];
}
/**
* @param string $fieldName
* @return string
*/
public function getAssociationMappedByTargetField($fieldName)
{
return $this->associationMappings[$fieldName]['mappedBy'];
}
}

View File

@ -24,6 +24,7 @@ use Exception, InvalidArgumentException, UnexpectedValueException,
Doctrine\Common\Collections\Collection,
Doctrine\Common\NotifyPropertyChanged,
Doctrine\Common\PropertyChangedListener,
Doctrine\Common\Persistence\ObjectManagerAware,
Doctrine\ORM\Event\LifecycleEventArgs,
Doctrine\ORM\Mapping\ClassMetadata,
Doctrine\ORM\Proxy\Proxy;
@ -1642,7 +1643,7 @@ class UnitOfWork implements PropertyChangedListener
// If there is no ID, it is actually NEW.
if ( ! $id) {
$managedCopy = $class->newInstance();
$managedCopy = $this->newInstance($class);
$this->persistNew($class, $managedCopy);
} else {
@ -1666,7 +1667,7 @@ class UnitOfWork implements PropertyChangedListener
throw new EntityNotFoundException;
}
$managedCopy = $class->newInstance();
$managedCopy = $this->newInstance($class);
$class->setIdentifierValues($managedCopy, $id);
$this->persistNew($class, $managedCopy);
@ -2157,6 +2158,18 @@ class UnitOfWork implements PropertyChangedListener
return isset($this->collectionsDeletions[spl_object_hash($coll)]);
}
/**
* @param ClassMetadata $class
*/
private function newInstance($class)
{
$entity = $class->newInstance();
if ($entity instanceof \Doctrine\Common\Persistence\ObjectManagerAware) {
$entity->injectObjectManager($this->em, $class);
}
return $entity;
}
/**
* INTERNAL:
* Creates an entity. Used for reconstitution of persistent entities.
@ -2209,6 +2222,11 @@ class UnitOfWork implements PropertyChangedListener
// If only a specific entity is set to refresh, check that it's the one
if(isset($hints[Query::HINT_REFRESH_ENTITY])) {
$overrideLocalValues = $hints[Query::HINT_REFRESH_ENTITY] === $entity;
// inject ObjectManager into just loaded proxies.
if ($overrideLocalValues && $entity instanceof ObjectManagerAware) {
$entity->injectObjectManager($this->em, $class);
}
}
}
@ -2216,7 +2234,7 @@ class UnitOfWork implements PropertyChangedListener
$this->originalEntityData[$oid] = $data;
}
} else {
$entity = $class->newInstance();
$entity = $this->newInstance($class);
$oid = spl_object_hash($entity);
$this->entityIdentifiers[$oid] = $id;
$this->entityStates[$oid] = self::STATE_MANAGED;

@ -1 +1 @@
Subproject commit 3052115f241020b67326cf1368540d33baa991f3
Subproject commit 9c880cf9ae2c14102568520b5ee885b03bda93e4

View File

@ -0,0 +1,105 @@
<?php
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Common\Persistence\PersistentObject;
/**
* Test that Doctrine ORM correctly works with the ObjectManagerAware and PersistentObject
* classes from Common.
*
* @group DDC-1448
*/
class PersistentObjectTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
try {
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\PersistentEntity'),
));
} catch (\Exception $e) {
}
PersistentObject::setObjectManager($this->_em);
}
public function testPersist()
{
$entity = new PersistentEntity();
$entity->setName("test");
$this->_em->persist($entity);
$this->_em->flush();
}
public function testFind()
{
$entity = new PersistentEntity();
$entity->setName("test");
$this->_em->persist($entity);
$this->_em->flush();
$this->_em->clear();
$entity = $this->_em->find(__NAMESPACE__ . '\PersistentEntity', $entity->getId());
$this->assertEquals('test', $entity->getName());
$entity->setName('foobar');
$this->_em->flush();
}
public function testGetReference()
{
$entity = new PersistentEntity();
$entity->setName("test");
$this->_em->persist($entity);
$this->_em->flush();
$this->_em->clear();
$entity = $this->_em->getReference(__NAMESPACE__ . '\PersistentEntity', $entity->getId());
$this->assertEquals('test', $entity->getName());
}
public function testSetAssociation()
{
$entity = new PersistentEntity();
$entity->setName("test");
$entity->setParent($entity);
$this->_em->persist($entity);
$this->_em->flush();
$this->_em->clear();
$entity = $this->_em->getReference(__NAMESPACE__ . '\PersistentEntity', $entity->getId());
$this->assertSame($entity, $entity->getParent());
}
}
/**
* @Entity
*/
class PersistentEntity extends PersistentObject
{
/**
* @Id @Column(type="integer") @GeneratedValue
* @var int
*/
protected $id;
/**
* @Column(type="string")
* @var string
*/
protected $name;
/**
* @ManyToOne(targetEntity="PersistentEntity")
* @var PersistentEntity
*/
protected $parent;
}