Implementing this domain:
you may consider two different approaches:
First approach
This approach uses association. If you choose this one, you should manually link appropriate (company or client) to the current user depending on some logic. Each User
should probably have only one of those field at any given point in time. Company or Client, not both.
User
/** @Entity */
class User
{
/**
* @ORM\column(type="string")
*/
protected password;
/**
* @ORM\column(type="array")
*/
protected roles;
/**
* @ORM\OneToOne(targetEntity="Company", mappedBy="user")
*/
protected Company;
/**
* @ORM\OneToOne(targetEntity="Client", mappedBy="user")
*/
protected Client;
}
Company
/** @Entity */
class Company
{
/**
* @ORM\column(type="string")
*/
protected city;
/**
* @ORM\OneToOne(targetEntity="User", inversedBy="company")
*/
protected user;
}
Client
/** @Entity */
class Client
{
/**
* @ORM\column(type="string")
*/
protected sex;
/**
* @ORM\OneToOne(targetEntity="User", inversedBy="client")
*/
protected user;
}
Second approach
This approach uses inheritance and seems to be more flexible, but has its own downsides.
User
/** @MappedSuperclass */
class User
{
/**
* @ORM\column(type="string")
*/
protected password;
/**
* @ORM\column(type="array")
*/
protected roles;
}
Company
/** @Entity */
class Company extends User
{
/**
* @Id @Column(type="integer")
*/
protected $id;
/**
* @ORM\column(type="string")
*/
protected city;
}
Client
/** @Entity */
class Client extends User
{
/**
* @Id @Column(type="integer")
*/
protected $id;
/**
* @ORM\column(type="string")
*/
protected sex;
}
You also have a One-To-Many relation between User & Message:
- One User can have Many Messages
- One Message belongs to only One User
Using First approach above is fine, but using the Second approach, you are getting yourself in trouble as Doctrine says:
persistent relationships defined by a mapped superclass must be unidirectional (with an owning side only). This means that One-To-Many associations are not possible on a mapped superclass at all.