dongyan3853 2017-03-18 15:51
浏览 63
已采纳

Symfony fosuserbundle将人员实体添加到个人实体

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"

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

The Account entity is the user class for FOSUserBundle

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

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

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

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

Thanks for your answers

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办