merged branch bschussek/issue4125 (PR #4953)

Commits
-------

17ca9b6 [Form] Fixed DoctrineType to use getManagerForClass() if no EM name is given

Discussion
----------

[Form] Fixed DoctrineType to use getManagerForClass() if no EM name is given

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #4125
Todo: -

---------------------------------------------------------------------------

by stof at 2012-07-17T08:16:59Z

👍
This commit is contained in:
Fabien Potencier 2012-07-17 11:07:53 +02:00
commit f52ce61782
3 changed files with 46 additions and 10 deletions

View File

@ -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

View File

@ -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,
));
}
/**

View File

@ -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');