2

I want to make a many-to-many association between a mapped superclass CommonResource and an entity Author. We are using the syntaxis provided by the doctrine manual on this topic: association override doctrine. But when I validate the doctrine schema with php bin/console doctrine:schema:val I get the following error:

[Mapping]  FAIL - The entity-class 'AppBundle\Entity\Author' mapping is invalid:
* The association AppBundle\Entity\Author#entities refers to the owning side field AppBundle\Entity\CommonResource#authors which does not exist.

I have a mapped superclass CommonResource:

/**
 * Class CommonResource
 * @ORM\MappedSuperclass()
 * @ORM\HasLifecycleCallbacks
 */
class CommonResource
{
...
/**
     * @var
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Author", inversedBy="entities")
     */
    protected $authors;
}

And an entity Author:

/**
 * Author
 *
 * @ORM\Table(name="author")
 * @ORM\HasLifecycleCallbacks
 */
class Author
{
...
/**
     * @var
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\CommonResource", mappedBy="authors")
     */
    protected $entities;
}

And some entities with extend the mapped superclass, for example:

/**
 * Itinerary
 *
 * @ORM\Table(name="itinerary")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\Itinerary\ItineraryRepository")
 * @ORM\AssociationOverrides({@ORM\AssociationOverride(name="authors", joinTable=@ORM\JoinTable(
 *              name="common_resource_itinerary",
 *              joinColumns=@ORM\JoinColumn(name="common_resource_id"),
 *              inverseJoinColumns=@ORM\JoinColumn(name="itinerary_id")
 *          ))})
 */
class Itinerary extends CommonResource implements AllowedPermissionInterface, EnrollmentInterface, SluggableInterface, CommonResourceEntityInterface
{
...
}

Have I used correctly the doctrine syntaxis for association overrides with mapped superclasses or there is something wrong? I would appreciate any help :)

  • A `MappedSuperclass` is like an `abstract class CommonResource` in PHP objects. In other words: you cannot reference it directly. You'll need something which implements that class, like `Itinerary`. As such, `@ORM\ManyToMany(targetEntity="AppBundle\Entity\Itinerary", mappedBy="authors")` would work. Are you sure you are wanting to use `MappedSuperclass` instead of [inheritance mapping](https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html), to me it would seem like you're trying to use MappedSuperclass as a discriminator. – rkeet Jun 21 '19 at 10:04
  • I see, but if I need to put the relation in every class that extends the superclass, there would be no need to put the superclass from the beginning. Isn't there a way to put the relation once instead of duplicating it for each child? – Ana Martín Jun 21 '19 at 12:48
  • Hence the link to inheritance mapping in my original comment ;-) Have a look at the examples there, you'll find it suits what you're looking for. Just need to decide on Single Table or Joined mapping types ;-) – rkeet Jun 21 '19 at 16:36

0 Answers0