FOSUserBundle: Get EntityManager instance overriding Form Handler

白昼怎懂夜的黑 提交于 2019-12-11 09:37:24

问题


I am starting with Symfony2 and I am trying to override FOS\UserBundle\Form\Handler\RegistrationFormHandler of FOSUserBundle.

My code is:

<?php

namespace Testing\CoreBundle\Form\Handler;

use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Form\Handler\RegistrationFormHandler as BaseHandler;
use Testing\CoreBundle\Entity\User as UserDetails;

class RegistrationFormHandler extends BaseHandler
{

    protected function onSuccess(UserInterface $user, $confirmation)
    {
        // I need an instance of Entity Manager but I don't know where get it!
        $em = $this->container->get('doctrine')->getEntityManager();
        // or something like: $em = $this->getDoctrine()->getEntityManager

        $userDetails = new UserDetails;
        $em->persist($userDetails);

        $user->setId($userDetails->getId());

        parent::onSuccess($user, $confirmation);
    }
}

So, the point is that I need an instance of Doctrine's Entity Manager but I don't know where/how get it in this case!

Any idea?

Thanks in advance!


回答1:


  1. You should not use EntityManager directly in most of the cases. Use a proper manager/provider service instead.

    In case of FOSUserBundle service implementing UserManagerInterface is such a manager. It is accessible through fos_user.user_manager key in the service container (which is an allias to fos_user.user_manager.default). Of course registration form handler uses that service, it is accessible through userManager property.

  2. You should not treat your domain-model (i.a. Doctrine's entities) as if it was exact representation of the database-model. This means, that you should assign objects to other objects (not their ids).

    Doctrine is capable of handling nested objects within your entities (UserDetails and User objects have a direct relationship). Eventually you will have to configure cascade options for User entity.

  3. Finally, UserDetails seems to be a mandatory dependency for each User. Therefore you should override UserManagerInterface::createUser() not the form handler - you are not dealing with user's details there anyway.

    1. Create your own UserManagerInterface implementation:

      class MyUserManager extends \FOS\UserBundle\Entity\UserManager {
          /**
           * {@inheritdoc}
           */
          public function createUser() {
              $user = parent::createUser();
      
              $user->setUserDetails(new UserDetails());
              // some optional code required for a proper
              // initialization of User/UserDetails object
              // that might require access to other objects
              // not available inside the entity
      
              return $user;
          }
      }
      
    2. Register your own manager as a serive inside DIC:

      <service id="my_project.user_manager" class="\MyProject\UserManager" parent="fos_user.user_manager.default" />
      
    3. Configure FOSUserBundle to use your own implementation:

       # /app/config/config.yml
       fos_user:
           ...
           service:
               user_manager: my_project.user_manager
      


来源:https://stackoverflow.com/questions/12574665/fosuserbundle-get-entitymanager-instance-overriding-form-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!