在 preUpdate Sonata Admin Bundle 中获取旧数据 [英] Get old data in preUpdate Sonata Admin Bundle
问题描述
我有一个 product
实体,它有一个 images
字段,用于存储产品中的图像名称,但图像名称取决于 part_number
字段是唯一的,所以如果用户在零件号中犯了错误并且他想编辑它,那么我也必须更改图像名称
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);
}
如何在 preUpdate() 函数中获取原始行?
How I get the original row in preUpdate() function?
推荐答案
根据官方 SonataAdmin google 论坛的主题:https://groups.google.com/forum/#!topic/sonata-devs/0zML6N13i3U您需要使用 UnitOfWork 类:http://www.doctrine-project.org/api/orm/2.3/class-Doctrine.ORM.UnitOfWork.html
According to the topic taken from the official SonataAdmin google forum: https://groups.google.com/forum/#!topic/sonata-devs/0zML6N13i3U you need to make use of the class UnitOfWork: http://www.doctrine-project.org/api/orm/2.3/class-Doctrine.ORM.UnitOfWork.html
这样做:
public function preUpdate($object)
{
$em = $this->getModelManager()->getEntityManager($this->getClass());
$original = $em->getUnitOfWork()->getOriginalDocumentData($object);
}
因此,您将获得数据库实体的值数组.例如:要访问您的实体的 password 值,请执行以下操作:
Thus you get an array of values of your database entity. E.g: to get access to the value password of your entity do:
$password = $original['password'];
仅此而已.享受:)
这篇关于在 preUpdate Sonata Admin Bundle 中获取旧数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!