Doctrine - ManyToMany关系没有正确删除

时间:2016-10-19 17:02:52

标签: php mysql symfony doctrine-orm

我使用链接表在两个实体之间存在多对多的关系。我的问题是,在删除时,似乎是另一个不应删除的实体正在尝试删除,而不是链接表中的行。

我有以下表格:

    CREATE TABLE `hotel_property` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`location_id` int(11) DEFAULT NULL,
`tier_id` int(11) DEFAULT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` longtext COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`)
)

CREATE TABLE `hotel_amenity` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`value_str` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`is_active` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
)

和链接表:

CREATE TABLE `hotel_property_amenity` (
`property_id` int(11) NOT NULL,
`amenity_id` int(11) NOT NULL,
PRIMARY KEY (`property_id`,`amenity_id`),
KEY `IDX_197E8A3E549213EC` (`property_id`),
KEY `IDX_197E8A3E9F9F1305` (`amenity_id`),
CONSTRAINT `FK_197E8A3E9F9F1305` FOREIGN KEY (`amenity_id`) REFERENCES `hotel_amenity` (`id`),
CONSTRAINT `FK_197E8A3E549213EC` FOREIGN KEY (`property_id`) REFERENCES `hotel_property` (`id`)
)

所以,我试图删除一个属性,该属性也应该删除hotel_property_amenity但我的日志显示正在删除hotel_amenity(或者它正在尝试但是它显然会引发约束违规)

日志:

500 An exception occurred while executing 'DELETE FROM hotel_amenity WHERE id = ?' with params [67]

Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`test`.`hotel_property_amenity`, CONSTRAINT `FK_197E8A3E9F9F1305` FOREIGN KEY (`amenity_id`) REFERENCES `hotel_amenity` (`id`))

Doctrine Property Entity(我认为我可能错了):

/**
 * @var ArrayCollection
 *
 * @ORM\ManyToMany(
 *        targetEntity="Test\Hotel\Entity\Amenity",
 *        indexBy="type",
 *        orphanRemoval=true,
 *        fetch="EXTRA_LAZY",
 *        cascade={"persist", "refresh", "remove"})
 * @ORM\JoinTable(name="hotel_property_amenity",
 *      joinColumns={@ORM\JoinColumn(name="property_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="amenity_id", referencedColumnName="id")})
 *
 * @ORM\OrderBy({"featured" = "DESC", "sortPosition" = "ASC"})
 */
protected $amenities;

有没有人知道我哪里出错了?

1 个答案:

答案 0 :(得分:0)

问题最终是级联="删除"。删除后,桥接表中的行将被处理,关联的实体将保持不变。