Symfony2 DoctrineExtensions preSoftDelete事件调用,不保存数据库中的自定义数据

时间:2014-06-06 09:35:42

标签: php symfony doctrine-orm doctrine

如果我是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');
        }

    }

}
?>

1 个答案:

答案 0 :(得分:1)

您应该保留您的对象并通知UoW您已进行了一些更改。你可以在你的条件下实现这一点:

 $em->persist($object);
 $classMetadata = $em->getClassMetadata(get_class($object));
 $em->getUnitOfWork()->computeChangeSet($classMetadata, $object);