Skip to content

Commit

Permalink
[Form] Fixed DoctrineType to use getManagerForClass() if no EM name i…
Browse files Browse the repository at this point in the history
…s given
  • Loading branch information
webmozart committed Jul 17, 2012
1 parent 151b79a commit 17ca9b6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
Expand Up @@ -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
27 changes: 18 additions & 9 deletions src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php
Expand Up @@ -55,19 +55,15 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
$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'])
Expand Down Expand Up @@ -96,7 +92,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
: $options['group_by'];

$hash = md5(json_encode(array(
spl_object_hash($manager),
spl_object_hash($options['em']),
$options['class'],
$propertyHash,
$loaderHash,
Expand All @@ -106,7 +102,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)

if (!isset($choiceListCache[$hash])) {
$choiceListCache[$hash] = new EntityChoiceList(
$manager,
$options['em'],
$options['class'],
$options['property'],
$options['loader'],
Expand All @@ -118,6 +114,15 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
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,
Expand All @@ -128,6 +133,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'choice_list' => $choiceList,
'group_by' => null,
));

$resolver->setFilters(array(
'em' => $emFilter,
));
}

/**
Expand Down
28 changes: 27 additions & 1 deletion src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
Expand Up @@ -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')) {
Expand All @@ -53,6 +61,7 @@ protected function setUp()
}

$this->em = DoctrineOrmTestCase::createTestEntityManager();
$this->emRegistry = $this->createRegistryMock('default', $this->em);

parent::setUp();

Expand Down Expand Up @@ -86,7 +95,7 @@ protected function tearDown()
protected function getExtensions()
{
return array_merge(parent::getExtensions(), array(
new DoctrineOrmExtension($this->createRegistryMock('default', $this->em)),
new DoctrineOrmExtension($this->emRegistry),
));
}

Expand Down Expand Up @@ -684,6 +693,23 @@ public function testSubmitCompositeStringIdentifier()
$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');
Expand Down

0 comments on commit 17ca9b6

Please sign in to comment.