普通网友 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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!