Search code examples
phpdatabasesymfonydoctrine-ormclass-table-inheritance

Multiple account-types?


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-usersand one for companies.

I've got an entity (table) called Account this table contains the username, passwordand saltcolumns.

If a company logs in I want to have the company information from a company-table and if a normal-userlogs 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 userretrieve 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?


Solution

  • 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
    {
        // ...
    }