Overriding inversedBy mapping in Doctrine 2 inheritance
doctrine-orm
Solution
You can override `inversedBy` since Doctrine 2.6. That would look like that:
/**
* @Entity
* @ORM\AssociationOverrides({
* @ORM\AssociationOverride(name="restaurant", inversedBy="collectionTimes")
* })
*/
class CollectionTime extends OrderTime
{
}
/**
* @Entity
* @ORM\AssociationOverrides({
* @ORM\AssociationOverride(name="restaurant", inversedBy="deliveryTimes")
* })
*/
class DeliveryTime extends OrderTime
{
}
Problem
I have the following entity: ``` class Restaurant { /** * @OneToMany(targetEntity="CollectionTime", mappedBy="restaurant") */ protected $collectionTimes; /** * @OneToMany(targetEntity="DeliveryTime", mappedBy="restaurant") */ protected $deliveryTimes; } ``` Mapping to two subclasses of the same entity: ``` /** * @Entity * @InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorMap({ * "CollectionTime" = "CollectionTime", * "DeliveryTime" = "DeliveryTime" * }) */ abstract class OrderTime { /** * @ManyToOne(targetEntity="Restaurant") */ protected $restaurant; } /** * @Entity */ class CollectionTime extends OrderTime { } /** * @Entity */ class DeliveryTime extends OrderTime { } ``` Now the problem is, `doctrine orm:validate-schema` reports the following errors: The field Restaurant#collectionTimes is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity CollectionTime#restaurant does not contain the required 'inversedBy=collectionTimes' attribute. The field Restaurant#deliveryTimes is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity DeliveryTime#restaurant does not contain the required 'inversedBy=deliveryTimes' attribute. In short, Doctrine expects every `mappedBy` to have an `inversedBy` on the other side. The only solution I can see so far is to move the `OrderTime::$restaurant` property and mapping to `CollectionTime` and `DeliveryTime`, just to be able to add the proper `inversedBy` mapping: ``` abstract class OrderTime { } /** * @Entity */ class CollectionTime extends OrderTime { /** * @ManyToOne(targetEntity="Restaurant", inversedBy="collectionTimes") */ protected $restaurant; } /** * @Entity */ class DeliveryTime extends OrderTime { /** * @ManyToOne(targetEntity="Restaurant", inversedBy="deliveryTimes") */ protected $restaurant; } ``` But it is cumbersome and goes against the principle of inheritance. Is there a way to just override the `inversedBy` attribute in the subclasses, without having to (re)declare the whole property in the subclass? I've looked into @AssociationOverrides and @AttributeOverrides, but they don't seem to be designed for this purpose.