php - Doctrine 2 association inheritance with different classes


I'm using Doctrine 2 and single table inheritance, my topmost entity hierarchy is:

JS_File --> [one to many] --> JS_File_Version

And then my sub-class entity hierarchy is:

JS_Image (extends JS_File) --> [one to many] --> JS_Image_Version (extends JS_File_Version)

The inheritance mapping works perfectly for JS_File and JS_Image, and the association mapping works perfectly for JS_File --> JS_File_Version. But, when it comes to map the "versions" association on JS_Image I get:

'Property "versions" in "JS_Image" was already declared, but it must be declared only once'

I understand why this is, the association is being inherited from JS_File and I can't overwrite it, but how do I achieve what I'm trying to do? Basically, with inheritance, how can I tell Doctrine to use a differenttargetEntity for the sub-classes association.




maybe what you are looking for is the @MappedSuperclass annotation on your JS_Image entity.

I think i used it some time ago, but according to the docs it allows you to "define state and mapping information that is common to multiple entity classes", and it can also "appear in the middle of an otherwise mapped inheritance hierarchy".

Maybe you can use this to tell Doctrine to see your JS_Image entity as the actual "parent" entity for your subentities. don't have time to test this now though, so please let me know if it helps.




Looks like what I'm trying to do isn't possible (having both JS_File and JS_Image as concrete entities). I've followed the advice of someone from the doctrine-user mailing list instead:

The way I solved this was to use an abstract base class and then define the actual mappings on the subclasses. For example, you could do something like:

JS_AbstractFile with subclasses: JS_File and JS_Image JS_AbstractVersion with subclasses: JS_FileVersion and JS_ImageVersion

AbstractFile and AbstractVersion can define the property (and getter/setters) but you shouldn't define any mapping information for the field. Then in your subclass you re-define the property along with mapping information (at least, thats how I did it since I used annotations)

People are also looking for solutions to the problem: api - SOAP PHP request error


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.