Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] 'em' option in entity form type #4125

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
kor3k opened this issue Apr 26, 2012 · 4 comments
Closed

[Form] 'em' option in entity form type #4125

kor3k opened this issue Apr 26, 2012 · 4 comments

Comments

@kor3k
Copy link
Contributor

kor3k commented Apr 26, 2012

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();    
    }
}
@stof
Copy link
Member

stof commented Apr 26, 2012

Passing the EntityManager itself is not a big use case as you will not have access to it anyway as soon as you nest types in a complex form, except by adding the option in all your parent types (which was needed in Symfony beta1 before the change to use the name, making it a pain to use a non-default manager).

However, using gteManagerForClass would be a valid improvement

@stof
Copy link
Member

stof commented Apr 26, 2012

And it should be done in DoctrineType directly to avoid duplicating the logic in each Doctrine bundle

@asm89
Copy link
Contributor

asm89 commented Jul 13, 2012

@JRFISH Do you want to work on a PR for this improvement?

@kor3k
Copy link
Contributor Author

kor3k commented Jul 16, 2012

@asm89 yes i will make a PR

fabpot added a commit that referenced this issue Jul 17, 2012
Commits
-------

17ca9b6 [Form] Fixed DoctrineType to use getManagerForClass() if no EM name is given

Discussion
----------

[Form] Fixed DoctrineType to use getManagerForClass() if no EM name is given

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #4125
Todo: -

---------------------------------------------------------------------------

by stof at 2012-07-17T08:16:59Z

:+1:
@fabpot fabpot closed this as completed Jul 17, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants