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

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

这些是我的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`))


谢谢

最佳答案

我不知道为什么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不考虑在ManyToMany关系中进行约束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49175339/

10-16 13:59