问题
I'm creating a website that should have at least two types of users, Company and Client, two of them have the same login form and different registration forms, and they can send messages to each other ...
Normaly (Without think about Doctrine) I think the database should look something like that:
User (id, email, password, facebook_id, roles)
Company(id, user_id, name, city, ...)
Client(id, user_id, name, sex ...)
Messages(id, sender_id(user_id), receiver_id(user_id), message, ...)
...
So now I want to know the best and the simplest way to implement this requirements using Doctrine and Symfony 4, How Entities could look like ?
(Note: I'm not using FOSUserBundle)
回答1:
Implementing this domain:
- User
- Company
- Client
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.
来源:https://stackoverflow.com/questions/53286674/symfony-doctrine-multiple-users-types