本文介绍了FOSUserBundle:Get EntityManager实例覆盖Form Handler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Symfony2开始,我试图覆盖FOSUserBundle的FOS\UserBundle\Form\Handler\RegistrationFormHandler。



我的代码是: / p>

 <?php 

命名空间Testing\CoreBundle\Form\Handler;

使用FOS\UserBundle\Model\UserInterface;
使用FOS\UserBundle\Form\Handler\RegistrationFormHandler作为BaseHandler;
使用Testing\CoreBundle\Entity\User作为UserDetails;

class RegistrationFormHandler extends BaseHandler
{

protected function onSuccess(UserInterface $ user,$ confirmation)
{
//我需要一个实体经理的实例,但我不知道在哪里得到它!
$ em = $ this-> container-> get('doctrine') - > getEntityManager();
//或类似的东西:$ em = $ this-> getDoctrine() - > getEntityManager

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

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

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

所以,关键是我需要一个Doctrine的实体经理,但我不知道在哪里/如何在这种情况下!



任何想法?



提前感谢!

解决方案

  1. 您不应该使用 EntityManager 直接在大多数情况下。使用适当的管理员/提供商服务。



    如果FOSUserBundle服务实现是这样的经理。它可以通过服务容器中的 fos_user.user_manager 键来访问(这是)。当然,注册表单处理程序,可以通过属性。


  2. 您不应该将您的域模型(ia Doctrine的实体)视为数据库模型。这意味着您应该将对象分配给其他对象(而不是其ID)。



    原则能够处理实体中的嵌套对象( UserDetails 用户对象有直接关系)。最终你将必须。


  3. UserDetails 似乎是每个用户的强制依赖关系。因此,您应该覆盖 UserManagerInterface :: createUser()而不是表单处理程序 - 您还没有处理用户的详细信息。


    1. 创建自己的 UserManagerInterface 实现:

        class MyUserManager extends \FOS\UserBundle\Entity\UserManager {
      / **
      * {@inheritdoc}
      * /
      public function createUser(){
      $ user = parent :: createUser();

      $ user-> setUserDetails(new UserDetails());
      //一个适当的
      所需的一些可选代码//初始化User / UserDetails对象
      //可能需要访问其他对象
      //在实体内不可用

      return $ user;
      }
      }


    2. 注册自己的经理作为一个serive里面DIC:

       < service id =my_project.user_managerclass =\MyProject\UserManagerparent =fos_user .user_manager.default/> 


    3. 配置FOSUserBundle以使用您自己的实现:

       #/app/config/config.yml 
      fos_user:
      ...
      服务:
      user_manager:my_project。 user_manager




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. 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
      

这篇关于FOSUserBundle:Get EntityManager实例覆盖Form Handler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 18:17