Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

DDC-1448 - Add support for ObjectManagerAware interface and Persisten…

…tObject in ORM
  • Loading branch information...
commit bda593a66dcceed8c32b25843c69289e6ba3cefd 1 parent 8eaf160
@beberlei beberlei authored
View
18 lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
@@ -1993,4 +1993,22 @@ public function getQuotedJoinTableName(array $assoc, $platform)
{
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
24 lib/Doctrine/ORM/UnitOfWork.php
@@ -24,6 +24,7 @@
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 @@ private function doMerge($entity, array &$visited, $prevManagedCopy = null, $ass
// 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 @@ private function doMerge($entity, array &$visited, $prevManagedCopy = null, $ass
throw new EntityNotFoundException;
}
- $managedCopy = $class->newInstance();
+ $managedCopy = $this->newInstance($class);
$class->setIdentifierValues($managedCopy, $id);
$this->persistNew($class, $managedCopy);
@@ -2158,6 +2159,18 @@ public function isCollectionScheduledForDeletion(PersistentCollection $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 @@ public function createEntity($className, array $data, &$hints = array())
// 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 @@ public function createEntity($className, array $data, &$hints = array())
$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;
2  lib/vendor/doctrine-common
@@ -1 +1 @@
-Subproject commit 3052115f241020b67326cf1368540d33baa991f3
+Subproject commit 9c880cf9ae2c14102568520b5ee885b03bda93e4
View
105 tests/Doctrine/Tests/ORM/Functional/PersistentObjectTest.php
@@ -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;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.