Details
Description
From
http://www.doctrine-project.org/documentation/manual/2_0/en/working-with-objects
on using persist() on an entity X:
- If X is a detached entity, an InvalidArgumentException will be thrown.
So I do this:
$picard = $this->_getUser('Picard');
$this->_em->persist($picard);
$this->_em->flush();
$this->_em->detach($picard);
$this->_em->persist($picard);
$this->_howMany("Picard", 1);
Result:
1) NakedPhp\Storage\DoctrineTest::testSavesUpdatedEntities
There are 2 instances of Picard saved instead of 1.
Failed asserting that <string:2> matches expected <integer:1>.
The entity is recognized as new instead of detached, so no error is thrown and it is duplicated in the database.
Issue Links
- relates to
-
DDC-455
E_NOTICE Undefined index when setting field to a property that is not persisted
-
Patch tentative. Resolved this bug but breaks another test of DetachedEntityTest, so I added another to make clear what is the issue: relying on keys to know if an entity is detached is not correct when entities have natural keys like CmsPhonenumber.
It seems that the only option would be hitting the database to see if the entity is already present.
Eliminating the error and simply persisting the entity anyway would not be cool because a duplicate will be created everytime entity is passed to persist(); even to pass it internally to merge() we have to know if it's new or detached.
A last option would be declaring: pass detached entities to persist at your own risk. It's normal for Doctrine not being aware of the difference between new and detached, if not 'detached' would not be so detached if the Orm remembers it. I have no problem in keeping new entities separated from detached ones in my data structure. Or I can simply merge() all, catching exceptions and add the entity that resulted in exceptions with persist().