Closed
Description
in
Symfony\Bridge\Doctrine\Form\Type\EntityType (DoctrineType)
the 'class' option is mandatory. if the class is managed by a non-default entity manager, then also an 'em' option must be set. and it takes a string entity manager name.
current behavior is if 'em' is omitted, default em is used. but since the class is specified, the 'em' should be get from
Doctrine\Common\Persistence\ManagerRegistry::getManagerForClass()
also would be nice if not only string, but also Doctrine\Common\Persistence\ObjectManager
can be passed here.
class EntityType extends DoctrineType
{
// ...
/**
* @param Options $options
* @return ObjectManager
*/
public function getEntityManager( Options $options )
{
if( null === $options['em'] )
{
$em = $this->registry->getManagerForClass( $options['class'] );
}
else if( $options['em'] instanceof ObjectManager )
{
$em = $options['em'];
}
else
{
$em = $this->registry->getManager( $options['em'] );
}
return $em;
}
public function getDefaultOptions()
{
$type = $this;
$loader = function (Options $options) use ($type) {
if (null !== $options['query_builder']) {
$manager = $type->getEntityManager($options);
return $type->getLoader($manager, $options['query_builder'], $options['class']);
}
return null;
};
$choiceList = function (Options $options) use ($type) {
$manager = $type->getEntityManager($options);
return new EntityChoiceList(
$manager,
$options['class'],
$options['property'],
$options['loader'],
$options['choices'],
$options['group_by']
);
};
return array(
'loader' => $loader,
'choice_list' => $choiceList,
) + parent::getDefaultOptions();
}
}