0

I am attempting to create a OneToMany relationship between two of my entities. One Customer can be a member of many Clubs.

These are my entities:

class Club
{
    /**
     * @ORM\ManyToOne(targetEntity="Customer", inversedBy="clubs")
     */
    private $customer;
class Customer
{
    /**
     * @var Club[]
     *
     * @ORM\OneToMany(targetEntity="Club", cascade={"persist"}, inversedBy="customer")
     * @ORM\JoinTable(name="customer_club",
     *      joinColumns={@ORM\JoinColumn(name="customer_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="club_id", referencedColumnName="id", unique=true)}
     * )
     */
    private $clubs;

Rather than adding a field to the club table, I have created a customer_club table which looks like:

_________________________
| customer_id | club_id |
_________________________
| 1           | 16      |
| 1           | 90      |
| 1           | 72      |
_________________________

These relationship definitions are wrong because when I load my Symfony 3.4 application I get the following error:

[Creation Error] The annotation @ORM\OneToMany declared on property AppBundle\Entity\Customer::$clubs does not have a property named "inversedBy". Available properties: mappedBy, targetEntity, cascade, fetch, orphanRemoval, indexBy

What have I done wrong?

crmpicco
  • 16,605
  • 26
  • 134
  • 210
  • 1
    check out the docs on this https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-unidirectional-with-join-table – Brucie Alpha Aug 15 '19 at 05:24
  • if you would read the error message, you would see that OneToMany can't have `inversedBy`, instead it has `mappedBy`. This is, because `Club` is the owning side and maps that relation with its `customer` property (which is what you tell Doctrine in the annotation on `Customer::$clubs`). Every relation must have an owning side, so naturally not both sides can have an `inversedBy`. – Jakumi Aug 15 '19 at 06:29
  • Thanks @BrucieAlpha the answer at https://stackoverflow.com/a/15444822/691505 and the docs posted helped with with this one. A uni-directional ManyToMany was the answer here. One crucial part I was missing was doing a `addClub` rather than `setClub` on the `Customer. – crmpicco Aug 15 '19 at 06:32
  • @crmpicco as far as i know you need to have either setEntities and getEntities or addEntity removeEntity getEntities methods available – Brucie Alpha Aug 15 '19 at 06:43

0 Answers0