Skip to content

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

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

Closed
geoffrey-brier opened this issue Jan 29, 2014 · 6 comments
Closed

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

geoffrey-brier opened this issue Jan 29, 2014 · 6 comments
Labels
Doctrine Feature Form Good first issue Ideal for your first contribution! (some Symfony experience may be required)

Comments

@geoffrey-brier
Copy link
Contributor

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?

@egeloen
Copy link

egeloen commented Jan 31, 2014

I'm the one who detect the issue and ask @geoffrey-brier to report it. After debugging it, IMO, there is two possibilities to fix it.

  1. Allow to pass an entity manager instance as em in all doctrine type and then update the normalizer accordingly. (Not sure if the fact it only allow string actually is intentional)
  2. Only allow string or null as em an then, update the normalizer with a is_string check.

@webmozart webmozart changed the title [Form] Exception thrown when the entity manager is already normalized [Form] Support passing EntityManager instances in "em" option Oct 17, 2014
@webmozart
Copy link
Contributor

Marked as feature request.

@webmozart webmozart added the Good first issue Ideal for your first contribution! (some Symfony experience may be required) label Oct 17, 2014
@egeloen
Copy link

egeloen commented Oct 17, 2014

@webmozart What would be your preference about my two propositions (allow an entity manager instance or only allow string)? I would prefer the first one :)

@webmozart
Copy link
Contributor

Both, no? Doing otherwise would break BC.

@egeloen
Copy link

egeloen commented Oct 17, 2014

Yeah obviously... When I said the entity manager, I meant the old way (string/null/entity manager) :) I will provide a PR this week-end.

@webmozart
Copy link
Contributor

Cool, thanks! :)

fabpot added a commit that referenced this issue Oct 20, 2014
…upport for em option (egeloen)

This PR was merged into the 2.6-dev branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #10157
| License       | MIT
| Doc PR        | symfony/symfony-docs#4336

This PR allows to pass an explicit entity manager instance as em option.

Commits
-------

d84e3a8 [Bridge][Doctrine][Form] Add entity manager instance support for em option
@fabpot fabpot closed this as completed Oct 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Doctrine Feature Form Good first issue Ideal for your first contribution! (some Symfony experience may be required)
Projects
None yet
Development

No branches or pull requests

4 participants