删除实体而不删除到它的外键链接

时间:2022-09-28 06:45:34

I have a table Products the entity tva is link to the table Tva and the entity image is link to the table Media. I render in sonata admin all the products of the website. I would like to be able to delete a product from the administration.

我有一个表产品实体tva链接到表tva,实体映像链接到表媒体。我在索纳塔管理所有产品的网站。我希望能够从管理部门删除一个产品。

I want be able to delete a product without to delete the foreign key link to it.

我希望能够删除一个产品而不删除外键链接到它。

But i have this error:

但我有一个错误:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (fly.post, CONSTRAINT FK_5A8A6C8D4D79775F FOREIGN KEY (tva_id) REFERENCES tva (id))

完整性约束违反:1451不能删除或更新父行:外键约束失败(fly)。后,约束FK_5A8A6C8D4D79775F外键(tva_id)引用tva (id)

This is my entity tva and image

这是我的实体tva和图像。

/**
 * @ORM\ManyToOne(targetEntity="FLY\BookingsBundle\Entity\Tva", cascade={"persist", "remove"})
 * @ORM\JoinColumn(nullable=true)
 */

private $tva;

/**
 * @ORM\ManyToOne(targetEntity="FLY\BookingsBundle\Entity\Media", cascade={"persist","remove"})
 * @ORM\JoinColumn(nullable=true)
 */
private $image;

I decided to change the join column from this * @ORM\JoinColumn(nullable=true) to this * @ORM\JoinColumn(onDelete="CASCADE") and it works i can delete the product but it also delete the foreign key and i don't want that because i have other product that use the same image and tva.

我决定将join列从这个* @ORM\JoinColumn(nullable=true)更改为这个* @ORM\JoinColumn(onDelete="CASCADE"),它可以工作我可以删除产品,但它也删除外键,我不想这样做,因为我有其他使用相同图像和tva的产品。

2 个解决方案

#1


4  

(onDelete="CASCADE") will delete the child record when parent record is deleted. If we don't want to delete the child record then we need to configure onDelete="SET NULL". This will set the foreign key value to null in child record.

(onDelete="CASCADE")删除父记录时将删除子记录。如果不想删除子记录,则需要配置onDelete=“SET NULL”。这将把外键值设置为子记录中的null。

#2


0  

I found a way to fix my problem :) I removed this line : cascade={"persist","remove"}and i changed this line * @ORM\JoinColumn(nullable=true) to * @ORM\joinColumn(onDelete="SET NULL")

我找到了一种解决问题的方法:)我删除了这行:cascade={"持久化","remove"},并将这行* @ORM\JoinColumn(nullable=true)改为* @ORM\JoinColumn(onDelete="SET NULL")

/**
 * @ORM\ManyToOne(targetEntity="FLY\BookingsBundle\Entity\Tva")
 * @ORM\joinColumn(onDelete="SET NULL")
 */
private $tva;

/**
 * @ORM\ManyToOne(targetEntity="FLY\BookingsBundle\Entity\Media")
 * @ORM\joinColumn(onDelete="SET NULL")
 */
private $image;

Thank you so much @Darshan Mehta for your help. :)

非常感谢darshan Mehta的帮助。:)

#1


4  

(onDelete="CASCADE") will delete the child record when parent record is deleted. If we don't want to delete the child record then we need to configure onDelete="SET NULL". This will set the foreign key value to null in child record.

(onDelete="CASCADE")删除父记录时将删除子记录。如果不想删除子记录,则需要配置onDelete=“SET NULL”。这将把外键值设置为子记录中的null。

#2


0  

I found a way to fix my problem :) I removed this line : cascade={"persist","remove"}and i changed this line * @ORM\JoinColumn(nullable=true) to * @ORM\joinColumn(onDelete="SET NULL")

我找到了一种解决问题的方法:)我删除了这行:cascade={"持久化","remove"},并将这行* @ORM\JoinColumn(nullable=true)改为* @ORM\JoinColumn(onDelete="SET NULL")

/**
 * @ORM\ManyToOne(targetEntity="FLY\BookingsBundle\Entity\Tva")
 * @ORM\joinColumn(onDelete="SET NULL")
 */
private $tva;

/**
 * @ORM\ManyToOne(targetEntity="FLY\BookingsBundle\Entity\Media")
 * @ORM\joinColumn(onDelete="SET NULL")
 */
private $image;

Thank you so much @Darshan Mehta for your help. :)

非常感谢darshan Mehta的帮助。:)