Symfony& FOSUserBundle:3个不同的配置文件,具体取决于角色 [英] Symfony & FOSUserBundle: 3 different profiles, depending on role
问题描述
我有一个用户类
# 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
在另一篇文章中 PUGXMultiUserBundle 被推荐。
我想使用 FOSUserBundle 来实现。有没有人知道如何通过角色定义不同的个人资料?
I'd like to use the FOSUserBundle to achieve that. Does anyone know how I can define different profiles by role?
提前感谢
推荐答案
有很多方法可以做到这一点。
There will be lots of ways to do this.
用户实体类包含所有字段,您的用户的基本形式类型仅包含所有三个角色的公共字段,然后将每个角色的角色添加到必要的字段中。
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.
基本形式类型
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
}
// ...
}
复杂的方式(我可能会这样走)
使用映射超类用户
,为每个角色扩展它。这将需要注意一方面(使用 Guard )。但是,当我讨厌FOSUserBundle的激情时,我可能偏向于这个解决方案。
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.
这篇关于Symfony& FOSUserBundle:3个不同的配置文件,具体取决于角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!