Reputation: 5937
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.
Upvotes: 2
Views: 10885
Reputation: 5937
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.
Upvotes: 4
Reputation: 382
transactional() is used to flush the operation. flush has made multiple time re
Upvotes: 2