Symfony fosuserbundle 将帐户实体添加到个人实体

Symfony fosuserbundle add Account entity to a person entity

我有一个带有 FOSUserBundle 的 Symfony 项目,我按照本指南“http://symfony.com/doc/master/bundles/FOSUserBundle/overriding_forms.html

在我的个人 Bundle 中扩展了 FormType

我用名字、姓氏、地址创建了一个 Person 实体,创建了它的 FormType,如下所示:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('firstName', TextType::class)
            ->add('lastName', TextType::class)
            ->add('adress', TextType::class)
            ->add('account', AccountType::class) ;
}

帐户实体是 FOSUserBundle

的用户 class

然后我为 Person 生成了 CRUD,newAction() 看起来像这样:

public function newAction(Request $request)
{
    $person = new Person();
    $form = $this->createForm('AppBundle\Form\PersonType', $person);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $person->getAccount()->setEnabled(1); //i'm doing this because it's not automatic
        $em->persist($person->getAccount());
        $em->persist($person);
        $em->flush($person);

        return $this->redirectToRoute('fos_user_security_login'); // redirecting to the login page because it's not done automatically
    }
    return $this->render('person/new.html.twig', array(
        'person' => $person,
        'form' => $form->createView(),
    ));
}

两个实体是一对一的关系,通过这段代码,它们都持久化在数据库中,我可以使用用户名和密码正常登录

这是我的 FOSUerBundle 配置:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: AppBundle\Entity\Account
    from_email:
        address: "%mailer_user%"
        sender_name: "%mailer_user%"
    registration:
        form:
            type: AppBundle\Form\AccountType

我想让用户在注册后自动登录,就像使用默认 FOSUserBundle 注册时发生的那样,有人知道该怎么做吗?

我尝试了很多东西但没有成功

感谢您的回答

我能够使用 $person->getAccount() 对象从我的控制器登录:

我所要做的就是像在默认控制器中一样发送 REGISTRATION_COMPLETED 事件:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/RegistrationController.php#L78

刚刚将一些代码复制到我的控制器

这将触发身份验证:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/EventListener/AuthenticationListener.php