gpt4 book ai didi

mysql - 为什么 Easyadmin 不考虑多对多关系中的约束

转载 作者:行者123 更新时间:2023-11-30 21:46:35 25 4
gpt4 key购买 nike

我有一个 ManyToMany 关系,如果我试图从 MySQL 中删除一个相关项,我会因错误而被阻止;相反,如果我尝试从 Easyadmin 中删除相同的项目,我不会被阻止。

我预期的行为也将被 Easyadmin(v. 1.16 和 Symfony v. 3.3.10)阻止。请帮助...

这些是我的 2 个实体:

铅:

[...]

/**
* @ORM\ManyToMany(targetEntity="LeadInterest", inversedBy="leads")
* @JoinTable(name="leads_interests",
* joinColumns={@ORM\JoinColumn(name="lead_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="interest_id", referencedColumnName="id")}
* )
* @ORM\OrderBy({"interestName": "ASC"})
*/
private $interests = null;

[...]

public function __construct() {
$this->interests = new ArrayCollection();
}

public function addInterest(LeadInterest $i)
{
if(!$this->interests->contains($i)) {
$this->interests->add($i);
}
}

public function removeInterest(LeadInterest $i)
{
$this->interests->removeElement($i);
}

public function getInterests()
{
return $this->interests;
}

[...]

LeadInterest:

[...]

/**
* @ORM\ManyToMany(targetEntity="Lead", mappedBy="interests")
*/
private $leads;

[...]

public function __construct() {

$this->leads = new ArrayCollection();
$this->lastUpdate = new \DateTime();

}

public function addLead(Lead $lead)
{
$this->leads[] = $lead;
return $this;
}

public function removeLead(Lead $lead)
{
$this->leads->removeElement($lead);
}

public function getLeads()
{
return $this->leads;
}

[...]

这是我尝试从 MySQL 中删除一个项目时的错误:

mysql> delete from leadInterest where id=6;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`app`.`leads_interests`, CONSTRAINT `FK_2135A27B5A95FF89` FOREIGN KEY (`interest_id`) REFERENCES `leadInterest` (`id`))

mysql> delete from lead where id=88;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`app`.`leads_interests`, CONSTRAINT `FK_2135A27B55458D` FOREIGN KEY (`lead_id`) REFERENCES `lead` (`id`))

谢谢

最佳答案

我不知道为什么 EasyAdmin 没有抛出任何错误,但您显然缺少 onDelete="CASCADE" 告诉 MySQL 也删除那些前键。

/**
* @ORM\ManyToMany(targetEntity="LeadInterest", inversedBy="leads")
* @JoinTable(name="leads_interests",
* joinColumns={@ORM\JoinColumn(name="lead_id", referencedColumnName="id", onDelete="CASCADE")},
* inverseJoinColumns={@ORM\JoinColumn(name="interest_id", referencedColumnName="id", onDelete="CASCADE")}
* )
* @ORM\OrderBy({"interestName": "ASC"})
*/

关于mysql - 为什么 Easyadmin 不考虑多对多关系中的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49175339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com