Symfony & FOSUserBundle: 3 different profiles, depending on role

孤街浪徒 提交于 2020-01-04 02:43:05

问题


I've a User class

# User
- id
- username
- password
- email

and three different roles with different profile fields:

# 1. Teacher 
- teachingSince
- aboutMe
- classes

# 2. Pupil
- class
- parents
- dateOfBirth
- grade

3. Parent
- children
- phoneNumber

In another post the PUGXMultiUserBundle was recommended.

I'd like to use the FOSUserBundle to achieve that. Does anyone know how I can define different profiles by role?

Thanks in advance!


回答1:


There will be lots of ways to do this.

Simplest

the user entity class contains all the fields, you have a base form-type for user which contains only common fields for all three 'roles', then extend those for each role adding the necessary fields.

Base form type

class UserType extends AbstractType {

  public function buildForm( FormBuilderInterface $builder, array $options ) {
      $builder->add('username', TextType::class, []);
      // etc etc
  }

  // ...
}

Teacher

class TeacherType extends UserType {

  public function buildForm( FormBuilderInterface $builder, array $options ) {
      parent::buildForm($builder, $options);

      $builder->add('teaching', DateType::class, []);
      // etc etc
  }

  // ...
}

complicated way (I would probably go this way)

use a mapped superclass User, extend it for each 'role'. This will need some attention on the auth side (use Guard). But as I hate FOSUserBundle with a passion I'm probably biased to this solution.



来源:https://stackoverflow.com/questions/37046048/symfony-fosuserbundle-3-different-profiles-depending-on-role

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!