inheritancedoctrine-odmmappedsuperclass

Doctrine ODM - Specify collection name for MappedSuperclass


Doctrine ODM has annotation (collection) to specify which name should be used for collection. It defaults to class name but can be changed easily.

However I have mapped superclass which is extended by other classes. Inheritance type is single collection and this collection is named after the base abstract class. How can I change this name?

Let's say I have class Base, that gets extended. But I want the collection to be named Items. I obviously don't want to change annotations in all extending classes as it is quite redundant and prone to error.


Solution

  • As stated in the documentation

    A mapped superclass cannot be a document and is not queryable.

    thus it can't be mapped to any collection. You need to have a class having @Document and @InheritanceType annotations specified from which further documents will extend (the class can be abstract). You can either put it in between your mapped superclass or just have it instead. Also thanks to having a @Document mapped class you'll be able to query it while having concrete classes back.