0

I have three entities: 'Usuario', 'Estudiante' and 'Administrador'. 'Usuario' represents the users in the application, that can be 'Estudiante' or 'Administrador'. How can I relate this?

I show you the three entities:

Entity 'Usuario'

/**
 * @ORM\Entity()
 * @DoctrineAssert\UniqueEntity(fields = {"id_usuario", "guid"})
*/
class Usuario
{

/**
 *  @ORM\Id
 *  @ORM\OneToOne(targetEntity="Estudiante", inversedBy="id_estudiante")
 *  @ORM\JoinColumn(name="usuario_id", referencedColumnName="id_usuario")
 *  @ORM\OneToOne(targetEntity="Administrador", inversedBy="id_administrador")
 *  @ORM\JoinColumn(name="usuario_id", referencedColumnName="id_usuario")
 **/
protected $id_usuario;

/**
 *  @ORM\Column(type="string")
*/
protected $guid; 

(...)

Entity 'Estudiante'

class Estudiante implements UserInterface
{
    /**
     *  @ORM\Id
     *  @ORM\Column(type="string",length=100)
     *  @Assert\NotBlank(message = "Por favor, indica tu email")
     *  @Assert\Email(message = "El email introducido no es válido",
     *                checkMX=true)
     **/
    protected $id_estudiante;

    /**
     *  @ORM\Column(type="string")
     *  @Assert\Length(min=4, max=8, minMessage = "La contraseña debe contener entre 6 y 12 caracteres", maxMessage = "La contraseña debe contener entre 6 y 12 caracteres")
    */
    protected $password;

    /**
     *  @ORM\Column(type="string")
    */
    protected $salt;

(...)

Entity 'Administrador'

/**
 * @ORM\Entity()
 * @DoctrineAssert\UniqueEntity(fields = {"id_estudiante", "guid"})
*/
class Administrador implements UserInterface
{
    /**
     *  @ORM\Id
     *  @ORM\Column(type="string",length=100)
     *  @Assert\NotBlank(message = "Por favor, indica tu email")
     *  @Assert\Email(message = "El email introducido no es válido",
     *                checkMX=true)
     **/
    protected $id_administrador;

    /**
     *  @ORM\Column(type="string")
     *  @Assert\Length(min=4, max=8, minMessage = "La contraseña debe contener entre 6 y 12 caracteres", maxMessage = "La contraseña debe contener entre 6 y 12 caracteres")
    */
    protected $password;

    /**
     *  @ORM\Column(type="string")
    */
    protected $salt;

(...)

Please, how can I do that? I need to separate 'Estudiante' and 'Administrador' because if someone is registered as an 'Estudiante' and later wants to became an 'Administrador', I only have to move the field from 'Administrador' to 'Estudiante'. So another questions would be... is there another way to do that change?

tshepang
  • 12,111
  • 21
  • 91
  • 136
bamalu
  • 25
  • 2
  • 8

1 Answers1

0

First you should take a look how you want design your database, as far as I see you should not add different passwords/salts for each of the entities - under prediction that users also have password field.

General Advise
I strongly advise you should take a look on Data Normalization and may look on some different existing data models.

Take a look here:

Step 1 - Design

I would go for a different approach as and see Usuario, Estudiante and Administrador as userType/UserGroups. This could look like this. Sample DB Design (quickdirty!) There would be three rows in your userType Table - (Usuario, Estudiante and Administrador).

The question you should ask yourself is if you want to have a user a oneToMany or a oneToOne Relation.
In case of last, a oneToOne relation would mean that a a users can only be only one userType which would mean my proposed would work. Another extensive example would be this link and picture below (for ref: https://dba.stackexchange.com/questions/8896/user-system-database-design): User System Database Design from a Q&A

If you want a oneToMany relation you can just add a field user_id to the userType table which would be mapped to users. This would give you the opportunity of using different userTypes at the same time.

Step 2 - Creating the right Entities

Additionally take a look on the Doctrine Project (http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html) for knowing how and when to use the right statements.

Hope this sheds lingt into the dark and good luck on your endeavors!

Community
  • 1
  • 1
s1x
  • 574
  • 4
  • 15
  • Hi @s1x, first of all, thanks for your answer. I think I have not explained correctly how the entities are relationed. There are only two user types: 'Estudiante' and 'Administrador'. Both have many common fields, that's why I create the 'Usuario' entity. So this entity, must have a relation either with 'Estudiante' or 'Administrador' entity. Also, I need to separate 'Estudiante' and 'Administrador' in two different entities to add the getRoles() method in each one. For 'Estudiante' it will return "array('ROLE_ESTUDIANTE');" and for 'Administrador' it will return "array('ROLE_ADMINISTRADOR');" – bamalu Jan 31 '14 at 07:24
  • Regarding the information you provided you still use users as main table where the basic information will be stored. Why you don't use groups/types for your database design? Think about it for a moment. In case of your getRoles() you can still use this later via the User Entity if you connect them via a relation. In case of PHP just define a getRoles() in your user class and use the both entities/repros to recieve the data and get the job done. – s1x Jan 31 '14 at 23:35
  • I have tried to define a class Role that implements RoleInterface, but it causes problems with php 5.4 and 5.5. Can you write an example of how assinging different roles to an User? Thanks in advanced! – bamalu Feb 02 '14 at 19:13
  • https://github.com/FriendsOfSymfony/FOSUserBundle take a look here espc. on the `/model` and `/controller` folders. A good overview about the Doctrine Config files you'll find here: https://github.com/FriendsOfSymfony/FOSUserBundle/tree/master/Resources/config/doctrine/model – s1x Feb 03 '14 at 06:33
  • I have taken a look at this bundle but I don't understand how to solve my problem with it. What I need is to add/detele roles to an user created with an entity that implements UserInterface. – bamalu Feb 03 '14 at 11:17