Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f52ce61

Browse files
committed
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 :+1:
2 parents 0f746a5 + 17ca9b6 commit f52ce61

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ CHANGELOG
88
* added a session storage for Doctrine DBAL
99
* DoctrineOrmTypeGuesser now guesses "collection" for array Doctrine type
1010
* DoctrineType now caches its choice lists in order to improve performance
11+
* DoctrineType now uses ManagerRegistry::getManagerForClass() if the option "em" is not set

src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,15 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
5555
$registry = $this->registry;
5656
$type = $this;
5757

58-
$loader = function (Options $options) use ($type, $registry) {
58+
$loader = function (Options $options) use ($type) {
5959
if (null !== $options['query_builder']) {
60-
$manager = $registry->getManager($options['em']);
61-
62-
return $type->getLoader($manager, $options['query_builder'], $options['class']);
60+
return $type->getLoader($options['em'], $options['query_builder'], $options['class']);
6361
}
6462

6563
return null;
6664
};
6765

68-
$choiceList = function (Options $options) use ($registry, &$choiceListCache, &$time) {
69-
$manager = $registry->getManager($options['em']);
70-
66+
$choiceList = function (Options $options) use (&$choiceListCache, &$time) {
7167
// Support for closures
7268
$propertyHash = is_object($options['property'])
7369
? spl_object_hash($options['property'])
@@ -96,7 +92,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
9692
: $options['group_by'];
9793

9894
$hash = md5(json_encode(array(
99-
spl_object_hash($manager),
95+
spl_object_hash($options['em']),
10096
$options['class'],
10197
$propertyHash,
10298
$loaderHash,
@@ -106,7 +102,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
106102

107103
if (!isset($choiceListCache[$hash])) {
108104
$choiceListCache[$hash] = new EntityChoiceList(
109-
$manager,
105+
$options['em'],
110106
$options['class'],
111107
$options['property'],
112108
$options['loader'],
@@ -118,6 +114,15 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
118114
return $choiceListCache[$hash];
119115
};
120116

117+
$emFilter = function (Options $options, $em) use ($registry) {
118+
/* @var ManagerRegistry $registry */
119+
if (null !== $em) {
120+
return $registry->getManager($em);
121+
}
122+
123+
return $registry->getManagerForClass($options['class']);
124+
};
125+
121126
$resolver->setDefaults(array(
122127
'em' => null,
123128
'class' => null,
@@ -128,6 +133,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
128133
'choice_list' => $choiceList,
129134
'group_by' => null,
130135
));
136+
137+
$resolver->setFilters(array(
138+
'em' => $emFilter,
139+
));
131140
}
132141

133142
/**

src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ class EntityTypeTest extends TypeTestCase
3232
const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity';
3333
const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdentEntity';
3434

35+
/**
36+
* @var \Doctrine\ORM\EntityManager
37+
*/
3538
private $em;
3639

40+
/**
41+
* @var \PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $emRegistry;
44+
3745
protected function setUp()
3846
{
3947
if (!class_exists('Symfony\Component\Form\Form')) {
@@ -53,6 +61,7 @@ protected function setUp()
5361
}
5462

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

5766
parent::setUp();
5867

@@ -86,7 +95,7 @@ protected function tearDown()
8695
protected function getExtensions()
8796
{
8897
return array_merge(parent::getExtensions(), array(
89-
new DoctrineOrmExtension($this->createRegistryMock('default', $this->em)),
98+
new DoctrineOrmExtension($this->emRegistry),
9099
));
91100
}
92101

@@ -684,6 +693,23 @@ public function testSubmitCompositeStringIdentifier()
684693
$this->assertSame('0', $field->getClientData());
685694
}
686695

696+
public function testGetManagerForClassIfNoEm()
697+
{
698+
$this->emRegistry->expects($this->never())
699+
->method('getManager');
700+
701+
$this->emRegistry->expects($this->once())
702+
->method('getManagerForClass')
703+
->with(self::SINGLE_IDENT_CLASS)
704+
->will($this->returnValue($this->em));
705+
706+
$this->factory->createNamed('name', 'entity', null, array(
707+
'class' => self::SINGLE_IDENT_CLASS,
708+
'required' => false,
709+
'property' => 'name'
710+
));
711+
}
712+
687713
protected function createRegistryMock($name, $em)
688714
{
689715
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');

0 commit comments

Comments
 (0)