Created at :
Friday 14 February 2020
Method for recovering old data from a FormType under Symfony
You need to recover old data from an Entity in Symfony to do some processing such as managing a history or other? I will present you a method that uses Unit Of Work to compare old data of data sent via the form created by FormType.
Use Unit Of Work from Doctrine
To retrieve old data it is necessary to retrieve Unit Of Work from the Doctrine manager :
$unit_of_work = $this->getDoctrine()->getManager()->getUnitOfWork();
Then we tell Unit Of Work to retrieve the current changes:
By retrieving changes with the computeChangeSets() function we can tell Unit Of Work to retrieve the "ChangeSet" of the desired entity, in my example an article :
$changeset = $uow->getEntityChangeSet($article);
And if we dump the variable $changeset with the dump() function we can see this data :
We see that I changed the title of my article, so I have in $changeset["title"] my old data and in $changeset["title"] the new data, same for the updatedAt we have the modification date before and after.
You now have everything you need to compare your old data, new data sent by FormType under Symfony thanks to Doctrine.