0

According to the documentation and this post I just need to specify onDelete="CASCADE" in the @ORM\JoinColumn and it should make a foreign key that cascades on delete. I did that..

/**
 * @ORM\OneToMany(targetEntity="OrderDates", mappedBy="order", cascade={"persist", "remove"}, orphanRemoval=TRUE)
 * @ORM\JoinColumn(name="order_id", referencedColumnName="id", onDelete="CASCADE")
 */
private $dates;

however when I make the migration, the FK it generates doesn't cascade on delete.

ALTER TABLE order_dates ADD CONSTRAINT FK_28EABDE38D9F6D38 FOREIGN KEY (order_id) REFERENCES orders (id)

I tried removing the , cascade={"persist", "remove"}, orphanRemoval=TRUE part too but to no avail.

Edit: I've also tried setting it on the inverse side but it doesn't work either (in fact NO foreign key is made at all when I do this):

/**
 * @ORM\ManyToOne(targetEntity="Orders", inversedBy="dates", cascade={"persist"})
 * @ORM\JoinColumn(name="order_id", referencedColumnName="id", onDelete="CASCADE")
 */
private $order;
Element Zero
  • 1,651
  • 3
  • 13
  • 31
  • Are you clearing your cache and pools after you make your changes in the annotations? As a note, using `onDelete=cascade` with `cascade=remove` is redundant. Since `cascade=remove` ignores the foreign key references by explicitly deleting the record. – Will B. Oct 12 '18 at 22:22
  • Yes I read that too. I did try clearing the cache - made no difference – Element Zero Oct 13 '18 at 00:19
  • What command are you using to create the migrations? Second note, using `cascade="remove"` on the `ManyToOne` relationship will delete the parent record. Typically you would put it in the `OneToMany` – Will B. Oct 13 '18 at 00:38
  • ok thanks! I'm just doing it through command like "bin/console make:migration" – Element Zero Oct 13 '18 at 06:04

1 Answers1

2

onDelete="CASCADE" works on the owning side (inversedBy), not the inverse side (mappedBy).

Preciel
  • 2,666
  • 3
  • 20
  • 45