普通网友 2011-05-06 14:12
浏览 59
已采纳

Doctrine 2删除实体问题

I am getting an odd bug when I am removing an entity using the entity manager. An exception is being thrown saying "A new entity was found through a relationship that was not configured to cascade persist operations" but I have no new entities. I think this may be a bug in Doctrine but figured I would ask around a bit first to make sure that I wasn't doing anything wrong before filling a report.

The relevant code:

<?php
$em = Doctrine\ORM\EntityManager::create(array('driver' => 'pdo_sqlite', 'memory' => true), $config);

$qb = $em
    ->getRepository('Bar')
    ->createQueryBuilder('bar1')
    ->select('bar1, foo1, bar2')
    ->join('bar1.foo', 'foo1')
    ->join('foo1.bar', 'bar2')
;

$qb->getQuery()->getResult();

$bar = $em->getRepository('Bar')->findOneBy(array('id' => 20));

$em->transactional(function($em) use ($bar) {
    $em->remove($bar);
    $em->flush();
});

If I remove the $em->transactional that wraps the remove and flush operation the remove works. Also, if I remove $qb->getQuery()->getResult(); for the previous query the remove and flush call works.

The error I am getting is:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'A new entity was found through a relationship that was not configured to cascade persist operations: Bar@0000000049dcca970000000025d8d6f9. Explicitly persist the new entity or configure cascading persist operations on the relationship.' in doctrine2-orm/lib/Doctrine/ORM/UnitOfWork.php:576
Stack trace:
  #0 doctrine2-orm/lib/Doctrine/ORM/UnitOfWork.php(495): Doctrine\ORM\UnitOfWork >computeAssociationChanges(Array, Object(Doctrine\ORM\PersistentCollection))
  #1 doctrine2-orm/lib/Doctrine/ORM/UnitOfWork.php(537): Doctrine\ORM\UnitOfWork->computeChangeSet(Object(Doctrine\ORM\Mapping\ClassMetadata), Object(Foo))
  #2 doctrine2-orm/lib/Doctrine/ORM/UnitOfWork.php(256): Doctrine\ORM\UnitOfWork->computeChangeSets()
  #3 doctrine2-orm/lib/Doctrine/ORM/EntityManager.php(334): Doctrine\ORM\UnitO in doctrine2-orm/lib/Doctrine/ORM/UnitOfWork.php on line 576

A complete working example can be found in this gist.

  • 写回答

2条回答 默认 最新

  • douruhu4282 2011-09-21 23:57
    关注

    I figured this out eventually after filling a bug report.

    transactional does a flush internally and the double flush was creating this issue. I don't think a double flush should break things but I didn't argue that point.

    With that said I wasn't using transactional correctly, Doctrine 2 has implicit transactions and there is no need to wrap database calls in transactional except under very specific instances.

    From the Doctrine 2 documentation

    Explicit transaction demarcation is required when you want to include custom DBAL operations in a unit of work or when you want to make use of some methods of the EntityManager API that require an active transaction. Such methods will throw a TransactionRequiredException to inform you of that requirement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了