Symfony fosuserbundle将“帐户"实体添加到个人实体 [英] Symfony fosuserbundle add Account entity to a person entity

查看:81
本文介绍了Symfony fosuserbundle将“帐户"实体添加到个人实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Symfony project with FOSUserBundle, i extended the FormType in my personal Bundle following this guide "http://symfony.com/doc/master/bundles/FOSUserBundle/overriding_forms.html"

我用名字,姓氏,地址...创建了一个Person实体,并按如下方式创建了它的FormType:

I created a Person entity with first name, last name, adresse ..., created its FormType like this :

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的用户类

The Account entity is the user class for FOSUserBundle

然后我为Person生成了CRUD,newAction()如下所示:

Then i generated the CRUD for Person, the newAction() looks like this :

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(),
    ));
}

这两个实体具有OneToOne关系,使用此代码,它们都保存在数据库中,并且我可以使用用户名和密码正常登录

The two entitys have a OneToOne relationship, with this code they are both persisted in the data base and i can use the username and password to log in normally

这是我的FOSUerBundle配置:

Here is my FOSUerBundle configuration :

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注册时一样,有人知道怎么做吗?

I want to the user to log in automatically after registration like it happens when using the default FOSUserBundle registration, anyone got an idea how to do so ?

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

I tried a lot of stuff but no success

感谢您的回答

推荐答案

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

I was able to login from my controller using the $person->getAccount() object:

我要做的就是像在默认Controller中一样调度REGISTRATION_COMPLETED事件: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/RegistrationController.php#L78

All i have to do is dispatch the REGISTRATION_COMPLETED event like in the default Controller : https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/RegistrationController.php#L78

只需将一些代码复制到我的控制器上

Just copied some code to my controller

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

这篇关于Symfony fosuserbundle将“帐户"实体添加到个人实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆