Brave publisher PUBLISHER
Web developer Front-End in Oise, France - Gary Deshayes

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 5 | Return an image via a controller route

Symfony

Created at : Wednesday 1 September 2021

VueJS 3, ExpressJS 4.17 | Uploading and resizing an image

Javascript Framework JS

Created at : Saturday 28 November 2020

Symfony Event Subscriber | Restricting an ip range with an Event Subscriber

Symfony

Created at : Saturday 14 November 2020

Angular, ExpressJS | JWT authentication with Angular 10 and ExpressJS 4 (MySQL)

Javascript SQL Framework JS

Created at : Sunday 20 September 2020

Symfony, Excel, CSV | Generating a CSV file for Excel with Symfony

Symfony

Created at : Thursday 6 August 2020

Symfony FormType Choice | Set data default to group of radio button

Symfony

Created at : Sunday 26 July 2020

Doctrine, Symfony | Difference between two dates in MySQL (Day, month, year...)

Symfony SQL

Created at : Thursday 18 June 2020

JavaScript, jQuery et Regex | Secure a password in real time with JS

Javascript jQuery

Created at : Monday 23 March 2020

Symfony, Doctrine | Retrieve old data of FormType

Symfony

Created at : Friday 14 February 2020

jQuery, Webpack and Symfony | Call jQuery in your twig files

Symfony Bugs

Created at : Sunday 26 January 2020