Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #76 from djlambert/multiple-references

Multiple references to entity
  • Loading branch information...
commit a28b6bde290b0a908d32938e3393244086c2d89d 2 parents 98e0592 + 5852f78
Guilherme Blanco guilhermeblanco authored
14 lib/Doctrine/Common/DataFixtures/Event/Listener/MongoDBReferenceListener.php
View
@@ -65,11 +65,15 @@ public function getSubscribedEvents()
public function postPersist(LifecycleEventArgs $args)
{
$object = $args->getDocument();
- if (($name = $this->referenceRepository->getReferenceName($object)) !== false) {
- $identity = $args->getDocumentManager()
- ->getUnitOfWork()
- ->getDocumentIdentifier($object);
- $this->referenceRepository->setReferenceIdentity($name, $identity);
+
+ if (($names = $this->referenceRepository->getReferenceNames($object)) !== false) {
+ foreach ($names as $name) {
+ $identity = $args->getDocumentManager()
+ ->getUnitOfWork()
+ ->getDocumentIdentifier($object);
+
+ $this->referenceRepository->setReferenceIdentity($name, $identity);
+ }
}
}
}
14 lib/Doctrine/Common/DataFixtures/Event/Listener/ORMReferenceListener.php
View
@@ -65,11 +65,15 @@ public function getSubscribedEvents()
public function postPersist(LifecycleEventArgs $args)
{
$object = $args->getEntity();
- if (($name = $this->referenceRepository->getReferenceName($object)) !== false) {
- $identity = $args->getEntityManager()
- ->getUnitOfWork()
- ->getEntityIdentifier($object);
- $this->referenceRepository->setReferenceIdentity($name, $identity);
+
+ if (($names = $this->referenceRepository->getReferenceNames($object)) !== false) {
+ foreach ($names as $name) {
+ $identity = $args->getEntityManager()
+ ->getUnitOfWork()
+ ->getEntityIdentifier($object);
+
+ $this->referenceRepository->setReferenceIdentity($name, $identity);
+ }
}
}
}
8 lib/Doctrine/Common/DataFixtures/ReferenceRepository.php
View
@@ -168,15 +168,15 @@ public function hasReference($name)
}
/**
- * Searches for a reference name in the
+ * Searches for reference names in the
* list of stored references
*
* @param object $reference
- * @return string
+ * @return array
*/
- public function getReferenceName($reference)
+ public function getReferenceNames($reference)
{
- return array_search($reference, $this->references, true);
+ return array_keys($this->references, $reference, true);
}
/**
25 tests/Doctrine/Tests/Common/DataFixtures/ProxyReferenceRepositoryTest.php
View
@@ -72,8 +72,8 @@ public function testReferenceIdentityPopulation()
->with('admin-role');
$referenceRepository->expects($this->once())
- ->method('getReferenceName')
- ->will($this->returnValue('admin-role'));
+ ->method('getReferenceNames')
+ ->will($this->returnValue(array('admin-role')));
$referenceRepository->expects($this->once())
->method('setReferenceIdentity')
@@ -131,4 +131,25 @@ public function testReferenceReconstruction()
$this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $ref);
}
+
+ public function testReferenceMultipleEntries()
+ {
+ $em = $this->getMockSqliteEntityManager();
+ $referenceRepository = new ProxyReferenceRepository($em);
+ $em->getEventManager()->addEventSubscriber(new ORMReferenceListener($referenceRepository));
+ $schemaTool = new SchemaTool($em);
+ $schemaTool->createSchema(array($em->getClassMetadata(self::TEST_ENTITY_ROLE)));
+
+ $role = new TestEntity\Role;
+ $role->setName('admin');
+
+ $em->persist($role);
+ $referenceRepository->addReference('admin', $role);
+ $referenceRepository->addReference('duplicate', $role);
+ $em->flush();
+ $em->clear();
+
+ $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $referenceRepository->getReference('admin'));
+ $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $referenceRepository->getReference('duplicate'));
+ }
}
25 tests/Doctrine/Tests/Common/DataFixtures/ReferenceRepositoryTest.php
View
@@ -71,8 +71,8 @@ public function testReferenceIdentityPopulation()
->with('admin-role');
$referenceRepository->expects($this->once())
- ->method('getReferenceName')
- ->will($this->returnValue('admin-role'));
+ ->method('getReferenceNames')
+ ->will($this->returnValue(array('admin-role')));
$referenceRepository->expects($this->once())
->method('setReferenceIdentity')
@@ -111,4 +111,25 @@ public function testReferenceReconstruction()
$this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $ref);
}
+
+ public function testReferenceMultipleEntries()
+ {
+ $em = $this->getMockSqliteEntityManager();
+ $referenceRepository = new ReferenceRepository($em);
+ $em->getEventManager()->addEventSubscriber(new ORMReferenceListener($referenceRepository));
+ $schemaTool = new SchemaTool($em);
+ $schemaTool->createSchema(array($em->getClassMetadata(self::TEST_ENTITY_ROLE)));
+
+ $role = new TestEntity\Role;
+ $role->setName('admin');
+
+ $em->persist($role);
+ $referenceRepository->addReference('admin', $role);
+ $referenceRepository->addReference('duplicate', $role);
+ $em->flush();
+ $em->clear();
+
+ $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $referenceRepository->getReference('admin'));
+ $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $referenceRepository->getReference('duplicate'));
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.