为什么Easyadmin不考虑ManyToMany关系中的约束

时间:2018-03-08 14:19:06

标签: mysql symfony doctrine-orm orm easyadmin

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

我的预期行为也会被Easyadmin阻止(使用Symfony v.3.3.10进行1.16版)。请帮忙......

这是我的两个实体:

铅:

[...]

/**
 * @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`))

由于

1 个答案:

答案 0 :(得分: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"})
 */