【发布时间】:2016-12-05 01:43:15
【问题描述】:
我会具体一点。 我有实体任务、城市和组织者。 当我尝试删除组织者时,出现与任务错误有关的问题
执行 'DELETE FROM Organizer WHERE id 时发生异常 = ?'带参数 [522]:
SQLSTATE[23000]:违反完整性约束:1451 无法删除或 更新父行:外键约束失败(
test.Quest, 约束FK_82D6D713876C4DDA外键 (organizer_id) 参考Organizer(id))
我完全不知道配置关系的正确方式。 逻辑是:
- 我们添加了新城市。
- 我们添加了新的组织者并将城市放入组织者中。
- 我们创建新任务,然后将城市和组织者放入其中。
- 如果我们删除组织者 - 任务中的组织者 ID 必须是 已移除(任务不得移除)。
- 如果我们删除组织者 - 必须删除城市中的组织者 ID 也是(并且不得删除城市)。
我很抱歉我的描述很愚蠢,但我试图排除误解。
那么,对于我的情况,我应该使用什么正确的实体关系?
class Quest {
...
/**
* @ORM\JoinColumn(name="organizer_id", referencedColumnName="id")
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Organizer")
*/
protected $organizer;
}
class Organizer {
...
/**
* @ORM\Column(type="string", length=140)
*/
protected $name;
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\City", inversedBy="organizers")
* @ORM\JoinTable(name="organizers_cities")
*/
protected $cities;
public function __construct() {
$this->quests = new ArrayCollection(); // i don't remember why this is here
}
}
class City {
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Organizer", mappedBy="cities")
*/
protected $organizers;
/**
* Constructor
*/
public function __construct() {
$this->quests = new \Doctrine\Common\Collections\ArrayCollection();
$this->organizers = new \Doctrine\Common\Collections\ArrayCollection();
}
}
【问题讨论】:
标签: doctrine-orm doctrine symfony symfony-2.1