Reputation: 318
I'm currently using Symfony2 and doctrine. and I'm working on a "create-account" page. There will be 2 different kinds of accounts avaliable on the page, one for normal-users
and one for companies
.
I've got an entity (table) called Account
this table contains the username
, password
and salt
columns.
If a company logs in I want to have the company information from a company
-table and if a normal-user
logs in I want the information from the user
-table.
I've been thinking about having a column named usertype
which tells me what type it is, then if the usertype is a company
retrieve the company-information-entity and if it's a user
retrieve the user-information-entity (corresponding to the id
of the account).
Is this a good way to solve this problem? or should I have 2 different tables? AccountUser and AccountCompany?
Upvotes: 1
Views: 131
Reputation: 13340
If you have many columns in Company table that don't exist in the User table and vice versa the best way to solve your problem is to use class-table inheritance which will create three tables: Account, User, Company. User and Company must be subclasses of Account entity.
According to link above you should have code like this:
namespace MyProject\Model;
/**
* @Entity
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="usertype", type="string")
* @DiscriminatorMap({"user" = "User", "company" = "Company"})
*/
class Account
{
// ...
}
/** @Entity */
class User extends Account
{
// ...
}
/** @Entity */
class Company extends Account
{
// ...
}
Upvotes: 2