RydelHouse
RydelHouse

Reputation: 362

More than one ManyToMany realtions - Doctrine, Symfony

I have two tables "RFQ" and "RFQItem". First ManyToMany relation I need to make a choose which RFQItems add to RFQ in RFQ form. Second ManyToMany relation I need to make a new RFQItems when creating new RFQ.

I have configured all, but schema update throws me an error:

The table with name 'rfqgroup.rfq_rfqitem' already exists.

Entity:

/**
 * @ORM\ManyToMany(targetEntity="RFQItem")
 * @ORM\JoinColumn(referencedColumnName="id", nullable=true)
 */
protected $rfqitem;

/**
 * @ORM\ManyToMany(targetEntity="RFQItem", cascade={"persist"})
 * @ORM\JoinColumn(referencedColumnName="rfq_id", nullable=true)
 */
protected $rfq_item_title;

It is even possible to make these relations?

Upvotes: 0

Views: 50

Answers (1)

sjagr
sjagr

Reputation: 16512

You must use the @JoinTable attribute to ensure that the Many-to-Many tables have unique names.

/**
 * @ORM\ManyToMany(targetEntity="RFQItem")
 * @ORM\JoinTable(name="rfq_rfqitem",
 *      joinColumns={@ORM\JoinColumn(name="rfq_item_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="rfqgroup_id", referencedColumnName="id")}
 * )
 */
public $rfqitem;

/**
 * @ORM\ManyToMany(targetEntity="RFQItem")
 * @ORM\JoinTable(name="rfq_rfqitem_title",
 *      joinColumns={@ORM\JoinColumn(name="rfq_item_title_id", referencedColumnName="rfq_id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="rfqgroup_id", referencedColumnName="id")}
 * )
 */
public $rfq_item_title;

You may need to correct the JoinColumn attributes for name and referencedColumnName depending on the variable names you've picked in each entity.

Upvotes: 2

Related Questions