如果我是var_dump($ object),则在对象中更新字段状态,但不保存在数据库中。
你能帮助我吗?
<?php
namespace Acme\MyBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
class SoftDeleteListener {
public function preSoftDelete(LifecycleEventArgs $args){
$em = $args->getEntityManager();
$object = $args->getEntity();
//if entity has field "status"
if($em->getClassMetadata(get_class($object))->hasField('status')){
//set the status with string "deleted"
$object->setStatus('deleted');
}
}
}
?>
答案 0 :(得分:1)
您应该保留您的对象并通知UoW您已进行了一些更改。你可以在你的条件下实现这一点:
$em->persist($object);
$classMetadata = $em->getClassMetadata(get_class($object));
$em->getUnitOfWork()->computeChangeSet($classMetadata, $object);