doctrine2约束违规 [英] doctrine2 constraint violation

查看:199
本文介绍了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;

当我尝试删除如下这样的对象:

When I try to remove an object like here:

$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;

我可以做任何我想要的,我收到以下错误:

I can do whatever I want, I get the following error:


使用params {1:3928}执行DELETE FROM book WHERE pkId =?时发生异常:SQLSTATE [23000]:违反完整性约束:1451无法删除或更新父行:外键约束失败( p1 ,CONSTRAINT FK_F51A442F78734022 FOREIGN KEY( fkstatistic )参考 bookstatistic pkId ))

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

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

Has anbybody an idea what I'm doing wrong? I tried a lot of methods but nothing helps.

推荐答案

如果有人遇到类似的问题,解决方案:

In case someone runs into a similar issue, here is the solution:

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

onDelete 选项将首先删除关系,然后doctrine将执行级联操作。

The onDelete option will remove the relation first and then doctrine will do the cascade operation.

这篇关于doctrine2约束违规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆