我有这个rekation以下实体:doctrine2违反约束
/**
* Acme\DemoBundle\Entity\Book
*
* @ORM\Table(name="book")
* @ORM\Entity(repositoryClass="Acme\DemoBundle\Repository\BookRepository")
* @ORM\HasLifecycleCallbacks
* @UniqueEntity(fields="publickey", groups={"publickey"})
*/
class P1guestlistentry {
/**
* @var P1guestlistentrystatistic
*
* @ORM\OneToOne(targetEntity="P1guestlistentrystatistic", orphanRemoval=true, cascade={"all"}, fetch="EAGER")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="fkstatistic", referencedColumnName="pkId", nullable=false)
* })
*/
private $fkstatistic;
当我尝试删除的对象喜欢这里:
$this->getEntityManager()->getConnection()->beginTransaction();
try{
$book = $this->getEntityManager()->getRepository('AchmeDemoBundle:Book')->find(3928);
$this->getEntityManager()->remove($book);
$this->getEntityManager()->flush();
$this->getEntityManager()->getConnection()->commit();
}catch(Exception $e){
$this->getEntityManager()->getConnection()->rollBack();
echo $e->getMessage();
}
exit;
我可以做我想做的事情,我得到以下错误:
An exception occurred while executing 'DELETE FROM book WHERE pkId = ?' with params {"1":3928}: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (
p1
.book
, CONSTRAINTFK_F51A442F78734022
FOREIGN KEY (fkstatistic
) REFERENCESbookstatistic
(pkId
))
有一个想法我做错了什么? 我尝试了很多方法,但没有任何帮助。
你的关联实体是什么样的?你确定你需要'orphanRemoval'和所有级联选项吗? – Ocramius 2013-02-12 11:16:03
的主要问题是,我必须删除统计对象,当我删除书,我想我可以做它whith级联删除或orphanremoval。我如何向你展示关联的实体? – 2013-02-12 11:33:05