Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8482477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:50:28+00:00 2026-06-10T19:50:28+00:00

I currently have a model structure as follows: /** * @ORM\Entity * @ORM\InheritanceType(JOINED) *

  • 0

I currently have a model structure as follows:

/**
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="related_type", type="string")
 * @ORM\DiscriminatorMap({"type_one"="TypeOne", "type_two"="TypeTwo"})
 */
abstract class BaseEntity {

    ... (all the usual stuff, IDs, etc)

    /**
     * @ORM\OneToMany(targetEntity="Comment", mappedBy="baseEntity")
     */
    private $comments;
}

/**
 * @ORM\Entity
 */
class TypeOne extends BaseEntity {
    /**
     * @ORM\Column(type="string")
     */
    private $name;

    /**
     * @ORM\Column(type="string")
     */
    private $description;
}

/**
 * @ORM\Entity
 */
class TypeTwo extends BaseEntity {
    /**
     * @ORM\Column(type="string")
     */
    private $name;

    /**
     * @ORM\Column(type="string")
     */
    private $description;   
}

/**
 * @ORM\Entity
 */
class Comment {

    ... (all the usual stuff, IDs, etc)

    /**
     * @ORM\ManyToOne(targetEntity="BaseEntity", inversedBy="comments")
     */
    private $baseEntity;
}

The idea here is to be able to tie a comment to any of the other tables. This all seems to be working ok so far (granted, I’m still exploring design options so there could be a better way to do this…), but the one thing I’ve noticed is that the subclasses have some common fields that I’d like to move into a common parent class. I don’t want to move them up into the BaseEntity as there will be other objects that are children of BaseEntity, but that won’t have those fields.

I’ve considered creating a MappedSuperclass parent class in the middle, like so:

/**
 * @ORM\MappedSuperclass
 */
abstract class Common extends BaseEntity {
    /**
     * @ORM\Column(type="string")
     */
    private $name;

    /**
     * @ORM\Column(type="string")
     */
    private $description;       
}

/**
 * @ORM\Entity
 */
class TypeOne extends Common {}

/**
 * @ORM\Entity
 */
class TypeTwo extends Common {}

I figured this would work, but the doctrine database schema generator is complaining that I can’t have a OneToMany mapping on a MappedSuperclass. I didn’t expect this to be a problem as the OneToMany mapping is still between the root BaseEntity and the Comment table. Is there a different structure I should be using, or other way to make these fields common without adding them on the BaseEntity?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:50:30+00:00Added an answer on June 10, 2026 at 7:50 pm

    From the Docs:

    A mapped superclass is an abstract or concrete class that provides
    persistent entity state and mapping information for its subclasses,
    but which is not itself an entity. Typically, the purpose of such a
    mapped superclass is to define state and mapping information that is
    common to multiple entity classes.

    That said, how can you associate one entity with one that is not?

    More from the docs:

    A mapped superclass cannot be an entity, it is not query-able and
    persistent relationships defined by a mapped superclass must be
    unidirectional (with an owning side only). This means that One-To-Many
    assocations are not possible on a mapped superclass at all.

    Furthermore Many-To-Many associations are only possible if the mapped
    superclass is only used in exactly one entity at the moment. For
    further support of inheritance, the single or joined table inheritance
    features have to be used.

    Source: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html


    Update

    Because your MappedSuperClass extends BaseEntity it also inherits the BaseEntity’s associations, as if it were its own. So you effectively DO have a OneToMany on a MappedSuperClass.

    To get around it, well, you’d need to modify/extend doctrine to work the way you want.

    As far as native functionality goes you have two options:

    Class Table Inheritance
    You Common class and the resulting DB representation would have the common fields and child classes will now only have the fields specific to themselves. Unfortunately this may be a misrepresentation of your data if you are simply trying to group common fields for the sake of grouping them.

    Make Common an Entity
    It appears that all a Mapped Super Class is is an Entity that isn’t represented in the DB. So, make common a Entity instead. The downside is that you’ll end up with a DB table, but you could just delete that.

    I recommend that you take a second look at your data and ensure that you are only grouping fields if they are common in both name and purpose. For example, a ComputerBox, a ShoeBox, a Man, and a Woman may all have the “height” property but in that case I wouldn’t suggest have a Common class with a “height” property that they all inherit from. Instead, I would have a Box with fields common to ComputerBox and ShoeBox and I’d have a Person with fields common to Man and Woman. In that situation Class Table Inheritance or single table if you prefer would work perfectly.

    If your data follows that example go with Single Table or Class Table Inheritance. If not, I might advise not grouping the fields.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to turn Model.attribute into Attribute.models. e.g. I currently have Model.first.attribute => string
I currently have a client that wants me to 'abstract' out a domain model
I currently have two different models: User and Project . The User model has
I currently have a DetailView for Django's built-in User . url( r'^users/(?P<pk>\d+)/$', DetailView.as_view( model
Currently I have a DateTime property for a model. I am using the telerik
I have a model User . The model currently works with a Register view
Currently I have a database of Courses, and it has 6 columns: class Course(models.Model):
I have the following project structure using a Domain Model, StructureMap and Fluent NHibernate:
I am currently working on a little application: The structure follows in some way
I have a model structure along the lines of: class Store(models.Model): STORE_TYPE = (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.