2013-02-12 142 views
1

我有这个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 , CONSTRAINT FK_F51A442F78734022 FOREIGN KEY (fkstatistic) REFERENCES bookstatistic (pkId))

有一个想法我做错了什么? 我尝试了很多方法,但没有任何帮助。

+0

你的关联实体是什么样的?你确定你需要'orphanRemoval'和所有级联选项吗? – Ocramius 2013-02-12 11:16:03

+0

的主要问题是,我必须删除统计对象,当我删除书,我想我可以做它whith级联删除或orphanremoval。我如何向你展示关联的实体? – 2013-02-12 11:33:05

回答

2

万一有人运行到类似的问题,这里是解决方案:

/** 
* @var statistic 
* 
* @ORM\OneToOne(targetEntity="statistic", cascade="ALL") 
* @ORM\JoinColumns({ 
* @ORM\JoinColumn(name="fkStatistic", referencedColumnName="pkId", onDelete="SET NULL") 
* }) 
*/ 

onDelete选项会先删除关系,然后主义会做级联操作。

+0

'onDelete'在顺序上处于db级别...考虑删除所有级联操作的混乱 – Ocramius 2013-02-12 13:48:55