Short and easy question:
How to determine that Doctrine PHP Entity
Object is new?
New - I mean is not in database and its not "update" action. Most important thing - I need way to check it without any query like:
$manager->findOne($something);
Actually sometimes I tried to check "is ID null" but Im not 100% sure this method is valid or not. And other tip or probably core of this question - I seen something like:
$manager->getUnitOfWork()->isInIdentityMap($object);
Looks nice but I can't find what actually do function isInIdentityMap
.
Is it true if it was persisted or removed? When this function say true and false?
//EDIT
I need to detect entity object created first time - existing only in php but not in database. Of course entity loaded from database to apply updates is not new for me - for example:
When you createing Comment
object you need to increment commentCounter
but it should be incremented only when you flushing this comment for first time.
You don't want to increment it when you updating or deleting existing comment.
How to check that comment is new and counter should be incremented.
Please explain in answers why your method is good/better
I checked some docs and tested some things and I hope that everything I write is correct:
Method
isInIdentityMap
is probably perfect for my case because it checks that my object exist in Identity Map, this map store only flushed objects or actually object with
ID
. New Object before persistence and after persist action still haveNULL
ID so its not in this map. This way as long as object was not flushed into database this method should work. (Map contains only objects loaded from database)Method to check
NULL
IDshould work too, its very similar to first methid but still Im not 100% sure it always work as I want and in addition we have first method so... Its better to use function prepared for it.
Method
contains
Do job but for other case. This method checks objects in entity manager but any persisted object, scheduled for any action is already tracked by
entityManager
. This function can help only as long as object is not persisted.Other methods
Unit of work have many other methods, I didnt check them all but it seems we can easly check many other things using functions like:
And other case - using Doctrine events event: