Skip to content

[Form] Support passing EntityManager instances in "em" option #10157

Closed
@geoffrey-brier

Description

@geoffrey-brier

Hi,

We are trying to replace a "entity" field by changing only options in an event subscriber.

So basically what we do is:

$form->add(
    $field->getName(),
    $field->getConfig()->getType()->getName(),
    array_merge($field->getConfig()->getOptions(), array('disabled' => true))
);

The problem is that it seems the ->getOptions() returns the normalized options and not the original ones. Thus the em option is normalized and em is not a string anymore but the concrete entity manager.

This is problematic because then the DoctrineType throws an exception because it only deals with strings or null values:

// Symfony\Bridge\Doctrine\Form\Type\DoctrineType;
$emNormalizer = function (Options $options, $em) use ($registry) {
            /* @var ManagerRegistry $registry */
            if (null !== $em) {
                return $registry->getManager($em); // This is where the exception is thrown
            }
            // ...

I think that the code below should probably fix the problem:

// Symfony\Bridge\Doctrine\Form\Type\DoctrineType;
$emNormalizer = function (Options $options, $em) use ($registry) {
            /* @var ManagerRegistry $registry */
            if (null !== $em) {
                if (is_object($em)) {
                    return $em;
                }
                return $registry->getManager($em);
            }
            // ...

What is your opinion?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DoctrineFeatureFormGood first issueIdeal for your first contribution! (some Symfony experience may be required)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions