Doctrine2: OneToMany on mapped superclass
Asked Answered
P

3

11

My DB structure is as follows:

work:

  • CTI table Work
  • MappedSuperclass table AbstractImageWork which extends Work
  • final table PhotoWork which extends AbstractImageWork

comment:

WorkComment has a ManyToOne relation to Work:

@ManyToOne(targetEntity="Work", inversedBy="comments")

Work has a OneToMany relation to WorkComment:

@OneToMany(targetEntity="WorkComment", mappedBy="work")

The problem is that Doctrine gives me this error while updating the schema:

[Doctrine\ORM\Mapping\MappingException]
It is illegal to put an inverse side one-to-many or many-to-many association on
mapped superclass 'Acme\...\AbstractImageWork#comments'.

I guess this has something to do with the MappedSuperclass AbstractImageWork stuck in the middle between Work and PhotoWork, but I didn't actually put this relation on the MappedSuperclass, but on the CTI table.. so why will Doctrine behave like this?

Any ideas?

Poll answered 29/10, 2012 at 15:3 Comment(0)
Z
25

In some cases, when you have such error when inherit from the class that is tagged as @ORM\MappedSuperclass, try to change your properties access level from private to protected

Z answered 15/11, 2013 at 11:30 Comment(1)
Well, strangely, this solution is not working for me. I’m running into the (almost exact) same issue and all my entities’ properties are already “protected”. I tried changing them to “public” to no avail.Freedafreedman
C
2

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 associations 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.

Check it out here: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/inheritance-mapping.html

Collectivize answered 18/12, 2020 at 10:4 Comment(0)
B
1

In others cases happens when you declare @ORM\Entity in an abstract superior class instead of @ORM\MappedSuperclass

Breannebrear answered 11/2, 2020 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.