diff --git a/src/Symfony/Bridge/Doctrine/CHANGELOG.md b/src/Symfony/Bridge/Doctrine/CHANGELOG.md index 30c3fe2f6f..c170225e5d 100644 --- a/src/Symfony/Bridge/Doctrine/CHANGELOG.md +++ b/src/Symfony/Bridge/Doctrine/CHANGELOG.md @@ -8,3 +8,4 @@ CHANGELOG * added a session storage for Doctrine DBAL * DoctrineOrmTypeGuesser now guesses "collection" for array Doctrine type * DoctrineType now caches its choice lists in order to improve performance + * DoctrineType now uses ManagerRegistry::getManagerForClass() if the option "em" is not set diff --git a/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php b/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php index 31216ea319..cc6aa38d88 100644 --- a/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php +++ b/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php @@ -55,19 +55,15 @@ abstract class DoctrineType extends AbstractType $registry = $this->registry; $type = $this; - $loader = function (Options $options) use ($type, $registry) { + $loader = function (Options $options) use ($type) { if (null !== $options['query_builder']) { - $manager = $registry->getManager($options['em']); - - return $type->getLoader($manager, $options['query_builder'], $options['class']); + return $type->getLoader($options['em'], $options['query_builder'], $options['class']); } return null; }; - $choiceList = function (Options $options) use ($registry, &$choiceListCache, &$time) { - $manager = $registry->getManager($options['em']); - + $choiceList = function (Options $options) use (&$choiceListCache, &$time) { // Support for closures $propertyHash = is_object($options['property']) ? spl_object_hash($options['property']) @@ -96,7 +92,7 @@ abstract class DoctrineType extends AbstractType : $options['group_by']; $hash = md5(json_encode(array( - spl_object_hash($manager), + spl_object_hash($options['em']), $options['class'], $propertyHash, $loaderHash, @@ -106,7 +102,7 @@ abstract class DoctrineType extends AbstractType if (!isset($choiceListCache[$hash])) { $choiceListCache[$hash] = new EntityChoiceList( - $manager, + $options['em'], $options['class'], $options['property'], $options['loader'], @@ -118,6 +114,15 @@ abstract class DoctrineType extends AbstractType return $choiceListCache[$hash]; }; + $emFilter = function (Options $options, $em) use ($registry) { + /* @var ManagerRegistry $registry */ + if (null !== $em) { + return $registry->getManager($em); + } + + return $registry->getManagerForClass($options['class']); + }; + $resolver->setDefaults(array( 'em' => null, 'class' => null, @@ -128,6 +133,10 @@ abstract class DoctrineType extends AbstractType 'choice_list' => $choiceList, 'group_by' => null, )); + + $resolver->setFilters(array( + 'em' => $emFilter, + )); } /** diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php index 38f4dc6695..aa17c4eca3 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php @@ -32,8 +32,16 @@ class EntityTypeTest extends TypeTestCase const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity'; const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdentEntity'; + /** + * @var \Doctrine\ORM\EntityManager + */ private $em; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $emRegistry; + protected function setUp() { if (!class_exists('Symfony\Component\Form\Form')) { @@ -53,6 +61,7 @@ class EntityTypeTest extends TypeTestCase } $this->em = DoctrineOrmTestCase::createTestEntityManager(); + $this->emRegistry = $this->createRegistryMock('default', $this->em); parent::setUp(); @@ -86,7 +95,7 @@ class EntityTypeTest extends TypeTestCase protected function getExtensions() { return array_merge(parent::getExtensions(), array( - new DoctrineOrmExtension($this->createRegistryMock('default', $this->em)), + new DoctrineOrmExtension($this->emRegistry), )); } @@ -684,6 +693,23 @@ class EntityTypeTest extends TypeTestCase $this->assertSame('0', $field->getClientData()); } + public function testGetManagerForClassIfNoEm() + { + $this->emRegistry->expects($this->never()) + ->method('getManager'); + + $this->emRegistry->expects($this->once()) + ->method('getManagerForClass') + ->with(self::SINGLE_IDENT_CLASS) + ->will($this->returnValue($this->em)); + + $this->factory->createNamed('name', 'entity', null, array( + 'class' => self::SINGLE_IDENT_CLASS, + 'required' => false, + 'property' => 'name' + )); + } + protected function createRegistryMock($name, $em) { $registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');