cw24
cw24

Reputation: 1733

Doctrine2 many-to-many relationship with multiple tables

I am using Symfony2 and Doctrine2 to build an application. One part of the application deals with addresses. I have one table "address" and three tables that connect to it with a many-to-many relationship (employee, client, supplier). I set this up with one connecting table "contact_address".

How do I create my entities with the many to many relationship? In my employee, client and supplier entities I have

/**
 * @var Addressess
 *
 * @ORM\ManyToMany(targetEntity="Address", mappedBy="contacts")
 */
private $addresses;

what do I put in my address entity?

    /**
     * @var Contacts
     *
     * @ORM\ManyToMany(targetEntity="Employee", inversedBy="id")
     * @ORM\JoinTable(name="contact_address",
     *   joinColumns={
     *      @ORM\JoinColumn(name="addressID", referencedColumnName="addressID")
     *   },
     *   inverseJoinColumns={
     *      @ORM\JoinColumn(name="employeeID", referencedColumnName="employeeID")
     *   }
     * )
     */
    private $contacts;

was my first thought, but what abot the clients and suppliers?

Upvotes: 1

Views: 1585

Answers (1)

Gabriel Theron
Gabriel Theron

Reputation: 1376

Can an address be linked to several persons at a time? That seems a bit weird to me...

In any case, I would advise you to have one entity Contact, on a One-to-Many relationship with Address, and either a one-to-one between Contact and Employee, Contact and Client, and Contact and Supplier, or have Employee, Client and Supplier inherit from Contact.

Upvotes: 2

Related Questions