1

我有一个多对多关系,如果我尝试从 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;
}

[...]

潜在客户:

[...]

/**
 * @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 中删除 une 项目时的错误:

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`))

谢谢

4

1 回答 1

0

我不知道为什么 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"})
 */
于 2018-03-08T16:00:25.947 回答