Get the old data in preUpdate SonataAdminBundle

1,315 views
Skip to first unread message

tttony

unread,
Apr 19, 2013, 1:00:29 AM4/19/13
to sonata...@googlegroups.com

I have a product entity and it has an images field that store the images names from the product but the images names depends of a part_number field that is unique, so if the user make a mistake in the part number and he wants to edit it then I also have to change the images names

I tried this but it does not works:

// 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 I get the original row in preUpdate() function?

Or there is other better way?

l3ia-et

unread,
Oct 3, 2014, 10:04:52 AM10/3/14
to sonata...@googlegroups.com
you have to use the doctrine listener

in the orm.yml
 entityListeners:
  l3ia\Bundle\ERPBundle\Listener\ReglementFactureListener:
   preUpdate: [preUpdateHandler]
   postUpdate: [postUpdateHandler]
   postRemove: [postRemoveHandler]



public function preUpdateHandler(ReglementFacture $rf, PreUpdateEventArgs $args)
{

if ($args->hasChangedField('facture'))
{
$this->old_facture = $args->getOldValue('facture');
}
}

M Khalid Junaid

unread,
Oct 4, 2014, 4:52:20 AM10/4/14
to sonata...@googlegroups.com
You can use Unit Of Work

As from docs You can get direct access to the Unit of Work by calling EntityManager#getUnitOfWork(). This will return the UnitOfWork instance the EntityManager is currently using.An array containing the original data of entity

I have answered on stackoverflow see below link for more details
 
Symfony 2 Get original data of entity from entity manager
Reply all
Reply to author
Forward
0 new messages