doctrine2约束违规

时间:2013-02-12 11:07:32

标签: php symfony doctrine-orm

我有以下实体:

/**
* 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;

我可以做任何我想做的事,我得到以下错误:

  

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

让人知道我做错了什么? 我尝试了很多方法,但没有任何帮助。

1 个答案:

答案 0 :(得分:2)

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

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

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