[Form] Fixed DoctrineType to use getManagerForClass() if no EM name is given
This commit is contained in:
parent
151b79a6ce
commit
17ca9b671a
@ -8,3 +8,4 @@ CHANGELOG
|
|||||||
* added a session storage for Doctrine DBAL
|
* added a session storage for Doctrine DBAL
|
||||||
* DoctrineOrmTypeGuesser now guesses "collection" for array Doctrine type
|
* DoctrineOrmTypeGuesser now guesses "collection" for array Doctrine type
|
||||||
* DoctrineType now caches its choice lists in order to improve performance
|
* DoctrineType now caches its choice lists in order to improve performance
|
||||||
|
* DoctrineType now uses ManagerRegistry::getManagerForClass() if the option "em" is not set
|
||||||
|
@ -55,19 +55,15 @@ abstract class DoctrineType extends AbstractType
|
|||||||
$registry = $this->registry;
|
$registry = $this->registry;
|
||||||
$type = $this;
|
$type = $this;
|
||||||
|
|
||||||
$loader = function (Options $options) use ($type, $registry) {
|
$loader = function (Options $options) use ($type) {
|
||||||
if (null !== $options['query_builder']) {
|
if (null !== $options['query_builder']) {
|
||||||
$manager = $registry->getManager($options['em']);
|
return $type->getLoader($options['em'], $options['query_builder'], $options['class']);
|
||||||
|
|
||||||
return $type->getLoader($manager, $options['query_builder'], $options['class']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
$choiceList = function (Options $options) use ($registry, &$choiceListCache, &$time) {
|
$choiceList = function (Options $options) use (&$choiceListCache, &$time) {
|
||||||
$manager = $registry->getManager($options['em']);
|
|
||||||
|
|
||||||
// Support for closures
|
// Support for closures
|
||||||
$propertyHash = is_object($options['property'])
|
$propertyHash = is_object($options['property'])
|
||||||
? spl_object_hash($options['property'])
|
? spl_object_hash($options['property'])
|
||||||
@ -96,7 +92,7 @@ abstract class DoctrineType extends AbstractType
|
|||||||
: $options['group_by'];
|
: $options['group_by'];
|
||||||
|
|
||||||
$hash = md5(json_encode(array(
|
$hash = md5(json_encode(array(
|
||||||
spl_object_hash($manager),
|
spl_object_hash($options['em']),
|
||||||
$options['class'],
|
$options['class'],
|
||||||
$propertyHash,
|
$propertyHash,
|
||||||
$loaderHash,
|
$loaderHash,
|
||||||
@ -106,7 +102,7 @@ abstract class DoctrineType extends AbstractType
|
|||||||
|
|
||||||
if (!isset($choiceListCache[$hash])) {
|
if (!isset($choiceListCache[$hash])) {
|
||||||
$choiceListCache[$hash] = new EntityChoiceList(
|
$choiceListCache[$hash] = new EntityChoiceList(
|
||||||
$manager,
|
$options['em'],
|
||||||
$options['class'],
|
$options['class'],
|
||||||
$options['property'],
|
$options['property'],
|
||||||
$options['loader'],
|
$options['loader'],
|
||||||
@ -118,6 +114,15 @@ abstract class DoctrineType extends AbstractType
|
|||||||
return $choiceListCache[$hash];
|
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(
|
$resolver->setDefaults(array(
|
||||||
'em' => null,
|
'em' => null,
|
||||||
'class' => null,
|
'class' => null,
|
||||||
@ -128,6 +133,10 @@ abstract class DoctrineType extends AbstractType
|
|||||||
'choice_list' => $choiceList,
|
'choice_list' => $choiceList,
|
||||||
'group_by' => null,
|
'group_by' => null,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
$resolver->setFilters(array(
|
||||||
|
'em' => $emFilter,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -32,8 +32,16 @@ class EntityTypeTest extends TypeTestCase
|
|||||||
const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity';
|
const COMPOSITE_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity';
|
||||||
const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdentEntity';
|
const COMPOSITE_STRING_IDENT_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeStringIdentEntity';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Doctrine\ORM\EntityManager
|
||||||
|
*/
|
||||||
private $em;
|
private $em;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
private $emRegistry;
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
if (!class_exists('Symfony\Component\Form\Form')) {
|
if (!class_exists('Symfony\Component\Form\Form')) {
|
||||||
@ -53,6 +61,7 @@ class EntityTypeTest extends TypeTestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->em = DoctrineOrmTestCase::createTestEntityManager();
|
$this->em = DoctrineOrmTestCase::createTestEntityManager();
|
||||||
|
$this->emRegistry = $this->createRegistryMock('default', $this->em);
|
||||||
|
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
@ -86,7 +95,7 @@ class EntityTypeTest extends TypeTestCase
|
|||||||
protected function getExtensions()
|
protected function getExtensions()
|
||||||
{
|
{
|
||||||
return array_merge(parent::getExtensions(), array(
|
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());
|
$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)
|
protected function createRegistryMock($name, $em)
|
||||||
{
|
{
|
||||||
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
|
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
|
||||||
|
Reference in New Issue
Block a user