1

My goal is to inherit an already inherited table with doctrine. I've got an abstract user class...

<?php
namespace UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;

/**
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap( {"student" = "Student", "employee" = "Employee", "customer" = "Customer"} )
 */
abstract class User extends BaseUser
{
}

...and various subclasses (student, customer, employee). Like this one:

<?php
namespace UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * UserBundle\Entity\Student
 *
 * @ORM\Entity
 */
class Student extends User
{
}

Within this setup everything is just working fine. Now other bundles, seen as individual modules, should add up columns from the sub-classes. Therefor I like to inherit from these classes like student. But those can't be abstract, because they are used by the core system.

Is it possible to solve this with doctrine class table inheritance? And if not, which solution would you recommend?

1 Answer 1

2

Sadly, it is not possible to have more than one level of inheritance in doctrine.

You may want to reconsider your design as such strong need for inheritance may be a sign of troubling news.

Let's take a the student for instance: By declearing this statement:

class Student extends User

you implicitly saying that all my student are also users. That may sound true but consider the scenario of a user witch is both a student and a teacher (like an university assistant).

Maybe there could be some sort of user profiling that relate your user to another entity. Some users may indeed lack of this relation as in the case of system bots. In this case your bundle/module can declare an entity and a a relationship with user (acting as bridge) that may further qualify your user.

I don't want to demolish your actual structure, if you explain better your needs i may provide with some insight.

Hope it make sense to you, Regards.

Sign up to request clarification or add additional context in comments.

2 Comments

Thx for the answer @francesco-panina. In my szenario indeed the user couldn't become both (student and employee) and for system bots I use a API key interface... those are independent from any user (except associated owners). My final workaround for the final thesis: I add the disciminator dynamically depending on the current module and merge the specific module user together with the used subclasse. It works... ugly... as hell...
To be frank most of the time I come to terms with reality and the implementation I choose is the most cost-effective, which is, far from the clean and elegant one... If it works for you it's no shame to pick any solution as long as it's an informed choice. good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.