Skip to content

[Bridge][Doctrine][Form] Add entity manager instance support for em option #12251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
$emNormalizer = function (Options $options, $em) use ($registry) {
/* @var ManagerRegistry $registry */
if (null !== $em) {
if ($em instanceof ObjectManager) {
return $em;
}

return $registry->getManager($em);
}

Expand Down Expand Up @@ -176,6 +180,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
));

$resolver->setAllowedTypes(array(
'em' => array('null', 'string', 'Doctrine\Common\Persistence\ObjectManager'),
'loader' => array('null', 'Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface'),
));
}
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ public function testClassOptionIsRequired()
$this->factory->createNamed('name', 'entity');
}

/**
* @expectedException Symfony\Component\Form\Exception\RuntimeException
*/
public function testInvalidClassOption()
{
$this->factory->createNamed('name', 'entity', null, array(
'class' => 'foo',
));
}

public function testSetDataToUninitializedEntityWithNonRequired()
{
$entity1 = new SingleIntIdEntity(1, 'Foo');
Expand Down Expand Up @@ -758,6 +768,21 @@ public function testGetManagerForClassIfNoEm()
));
}

public function testExplicitEm()
{
$this->emRegistry->expects($this->never())
->method('getManager');

$this->emRegistry->expects($this->never())
->method('getManagerForClass');

$this->factory->createNamed('name', 'entity', null, array(
'em' => $this->em,
'class' => self::SINGLE_IDENT_CLASS,
'property' => 'name',
));
}

protected function createRegistryMock($name, $em)
{
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
Expand Down