Get old data in preUpdate Sonata Admin Bundle

I have a product object and it has an images field that stores the names of images from the product, but the names of the images depend on the part_number field, which is unique, so if the user makes an error in the part number and he wants to edit it, then I also need to change image names

I tried this, but it does not work:

 // class ProductsAdmin extends Admin public function preUpdate($product) { $old_product = $this->getSubject(); if ($old_product->getPartNumber() != $product->getPartNumber) { // change file names } $this->saveFile($product); } 

How to get the source string in the preUpdate () function?

+7
source share
2 answers

In accordance with the theme adopted on the official google SonataAdmin forum: https://groups.google.com/forum/#!topic/sonata-devs/0zML6N13i3U you need to use the UnitOfWork class: http://www.doctrine-project.org /api/orm/2.3/class-Doctrine.ORM.UnitOfWork.html

Do this:

 public function preUpdate($object) { $em = $this->getModelManager()->getEntityManager($this->getClass()); $original = $em->getUnitOfWork()->getOriginalDocumentData($object); } 

This way you get an array of values ​​for your database object. For example: to access the password value of your object, follow these steps:

 $password = $original['password']; 

It's all. Enjoy :)

+13
source

If you just do the doctrine request in the preUpdate function to get the product from the database, you will have an old object. Then make a comparison and you will go well.

0
source

All Articles