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 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥30 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile