Doctrine2 + Symfony2保留每次插入,更新,删除的历史记录 [英] Doctrine2 + Symfony2 Keeping history on every insert, update, delete

查看:24
本文介绍了Doctrine2 + Symfony2保留每次插入,更新,删除的历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Web应用程序.最重要的功能应该是历史记录(版本控制)并由用户批准插入的数据.这意味着用户可以看到所有历史记录的更改,可以再次使用旧版本,可以验证或拒绝由用户插入的新版本,而不会造成干扰.

I'm developing web application. The most important feature should be history(versioning) and approving inserted data by user. It means that user can see all history changes, use old version again, validate or refuse a new version, which is inserted by user without permision.

我有一个表actionHistory(date,user,action,isValid)然后有例如人员,地址,CustomField和CustomValue表.人多对多地址.人员oneToMany CustomFields.CustomFields oneToOne CustomValue.每个表也都有一个历史表.

I have one table actionHistory(date,user,action,isValid) then I have e.g. Person, Address, CustomField and CustomValue tables. Person manyToMany Address. Person oneToMany CustomFields. CustomFields oneToOne CustomValue. Every table also have a historical table.

E.G.当我坚持使用自定义字段值的人时.我必须创建个人,customfield-value历史记录表.将此版本复制到其中.连接所有历史表,并同时连接 actionHistory .

E.G. When I am persisting person with custom field-value. I have to create person,customfield-value history tables. Copy this versions into them. Connect all historical tables and also connect with actionHistory.

开始时,我使用了EventListener和Lifecycle Event(postPersist,postDelete,postUpdate),但是主要的问题是,当我想添加前面的示例时,我总是遇到连接这些表的问题.我不知道哪个实体会先保留,而当我尝试第二个实体时尚不存在,等等.

At the beginning I've used EventListener and Lifecycle Event (postPersist,postDelete,postUpdate), but the main problem is that when I would like to add previous example I have always problem to connect these tables. I don't know which entity will persist first and when I try the second entity doesn't exist yet,etc.

public function postUpdate(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();
    $em = $args->getEntityManager();

    if ($entity instanceof Address) {
        $this->addPersonHistory($em, $entity);
        $this->saveAddressHistory("UPDATE", $em, $entity, "Address");
    }
    if ($entity instanceof CustomField) {
        $this->addCustomFieldHistory($em, $entity);
        $this->saveCustomFieldHistory("UPDATE", $em, $entity, "CustomField");
    }
    if ($entity instanceof CustomValue) {
        $this->saveCustomValueHistory("UPDATE", $em, $entity, "CustomValue");
    }
    if ($entity instanceof Person) {
        $this->savePersonHistory("UPDATE", $em, $entity, "Person");
    }
}

对于这种情况是否有更好的解决方案?因为我现在陷入困境,所以当我不得不面对添加person-customField-customValue的表单时.

Is there some better solution for this situation? Because I am stuck now, when I have to face form adding of person-customField-customValue.

提前谢谢!

推荐答案

您正在寻找的东西已经存在(好消息).

What you are looking for exists (good news).

这是通过存储库提供的Gedmo的 DoctrineExtensions 完成的: https://github.com/Atlantic18/DoctrineExtensions 您需要的扩展名是 Loggable ,并在此处进行了说明: https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/loggable.md

It's done with the DoctrineExtensions of Gedmo, provided by repository : https://github.com/Atlantic18/DoctrineExtensions The extension you need is Loggable, and explained here : https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/loggable.md

最好的是,有一个捆绑包可以直接实现这些扩展: https://github.com/stof/StofDoctrineExtensionsBundle 此处的说明: https://github.com/stof/StofDoctrineExtensionsBundle/blob/master/Resources/doc/index.rst

And the best is that there is a bundle that directly implements those extensions : https://github.com/stof/StofDoctrineExtensionsBundle Explanations here : https://github.com/stof/StofDoctrineExtensionsBundle/blob/master/Resources/doc/index.rst

这篇关于Doctrine2 + Symfony2保留每次插入,更新,删除的历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆