Mr. B.
Mr. B.

Reputation: 8697

Symfony & FOSUserBundle: 3 different profiles, depending on role

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!

Upvotes: 3

Views: 435

Answers (1)

DevDonkey
DevDonkey

Reputation: 4880

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.

Upvotes: 2

Related Questions