问题描述
我从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的实体经理,但我不知道在哪里/如何在这种情况下!
任何想法?
提前感谢!
-
您不应该使用
EntityManager
直接在大多数情况下。使用适当的管理员/提供商服务。
如果FOSUserBundle服务实现是这样的经理。它可以通过服务容器中的
fos_user.user_manager
键来访问(这是)。当然,注册表单处理程序,可以通过属性。 -
您不应该将您的域模型(ia Doctrine的实体)视为数据库模型。这意味着您应该将对象分配给其他对象(而不是其ID)。
原则能够处理实体中的嵌套对象(
UserDetails
和用户
对象有直接关系)。最终你将必须。 -
UserDetails
似乎是每个用户
的强制依赖关系。因此,您应该覆盖UserManagerInterface :: createUser()
而不是表单处理程序 - 您还没有处理用户的详细信息。-
创建自己的
UserManagerInterface
实现:class MyUserManager extends \FOS\UserBundle\Entity\UserManager {
/ **
* {@inheritdoc}
* /
public function createUser(){
$ user = parent :: createUser();
$ user-> setUserDetails(new UserDetails());
//一个适当的
所需的一些可选代码//初始化User / UserDetails对象
//可能需要访问其他对象
//在实体内不可用
return $ user;
}
}
-
注册自己的经理作为一个serive里面DIC:
< service id =my_project.user_managerclass =\MyProject\UserManagerparent =fos_user .user_manager.default/>
-
配置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!
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 throughfos_user.user_manager
key in the service container (which is an allias tofos_user.user_manager.default
). Of course registration form handler uses that service, it is accessible throughuserManager
property.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
andUser
objects have a direct relationship). Eventually you will have to configure cascade options forUser
entity.Finally,
UserDetails
seems to be a mandatory dependency for eachUser
. Therefore you should overrideUserManagerInterface::createUser()
not the form handler - you are not dealing with user's details there anyway.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; } }
Register your own manager as a serive inside DIC:
<service id="my_project.user_manager" class="\MyProject\UserManager" parent="fos_user.user_manager.default" />
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!