Symfony & FOSUserBundle:3 个不同的配置文件,取决于角色

Symfony & FOSUserBundle: 3 different profiles, depending on role

我有一个 用户 class

# User
- id
- username
- password
- email

和三个不同的角色,具有不同的个人资料字段

# 1. Teacher 
- teachingSince
- aboutMe
- classes

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

3. Parent
- children
- phoneNumber

another post the PUGXMultiUserBundle 中被推荐。

我想使用 FOSUserBundle 来实现。有谁知道如何按角色定义不同的配置文件?

提前致谢!

有很多方法可以做到这一点。

最简单

用户实体 class 包含所有字段,您有一个用户的基本表单类型,它只包含所有三个 'roles' 的公共字段,然后为每个角色扩展这些字段,添加必要的字段。

基本表单类型

class UserType extends AbstractType {

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

  // ...
}

老师

class TeacherType extends UserType {

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

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

  // ...
}

复杂的方式(我可能会走这条路)

使用映射的超级class User,为每个 'role' 扩展它。这在授权方面需要一些注意(使用 Guard)。但是由于我非常讨厌 FOSUserBundle,所以我可能偏向于这个解决方案。