Symfony, Doctrine | Retrieve old data of FormType

Web developer Front-End in Oise, France - Gary Deshayes <--Traduction

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:

$uow->computeChangeSets();

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 :
old data formtype symfony doctrine

We see that I changed the title of my article, so I have in $changeset["title"][0] my old data and in $changeset["title"][1] 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.

News publications :
Symfony

Created at : Monday 1 November 2021

Javascript Framework JS

Created at : Sunday 31 October 2021

Symfony

Created at : Wednesday 1 September 2021

Javascript Framework JS

Created at : Saturday 28 November 2020

Javascript SQL Framework JS

Created at : Sunday 20 September 2020

Symfony

Created at : Thursday 6 August 2020

Javascript jQuery

Created at : Monday 23 March 2020

Symfony

Created at : Friday 14 February 2020

Symfony Bugs

Created at : Sunday 26 January 2020