Check if entity was already persisted to EntityManager in Symfony2 Check if entity was already persisted to EntityManager in Symfony2 symfony symfony

Check if entity was already persisted to EntityManager in Symfony2


yes you shoud use the unitOfWork http://phpdox.de/demo/Symfony2/classes/Doctrine_ORM_UnitOfWork.xhtml#isEntityScheduled

$uow = $this->getDoctrine()->getManager()->getUnitOfWork()$exist =  $uow->isEntityScheduled(  $entity );


You could add a serialize method in your object that would create a string of the pertinent data in your model like..

public function serialize(){    return serialize(array(        $this->field1,        $this->field2,        .. etc ..    ));}

And then before persisting use the scheduled entity insertions to check whether the serialized data has been used before like..

$uow = $this->getDoctrine()->getManager()->getUnitOfWork()$insertions = $uow->getScheduledEntityInsertions();$insertions = new ArrayCollection($insertions);$scheduled = $insertions->filter(        function(YourModel $model) use ($new) {            return $model->serialize() === $new->serialize();        }    )->first();// This will either return the relevant model or false