Symfony3将每个更新保存到数据库中 [英] Symfony3 save every update into database

查看:65
本文介绍了Symfony3将每个更新保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是symfony和postgreSQL的新手,我试图保存数据库更新/删除日志
我创建了此表

I´m new on symfony and postgreSQL and I´m trying to save a log of database updates/deletes I´ve created this table

CREATE TABLE  "public"."changes" (
  "id" INT NOT NULL ,
  "id_user" INT NOT NULL,
  "operation" VARCHAR(45) NOT NULL,
  "table" VARCHAR(45) NOT NULL,
  "column" VARCHAR(45) NOT NULL,
  "old_data" TEXT NOT NULL,
  "new_data" TEXT NOT NULL,
  "ts" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY ("id"),
  CONSTRAINT "user"
    FOREIGN KEY ("id_user")
    REFERENCES "public"."users" ("id")
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

我需要的是每次我像这样总结时都保存到该表中

And what I need is to save into this table every time I do sumething like

/**
 * @Route("/insert", name="insert")
 */
public function insertAction(Request $request)
{
    $em = $this->get('doctrine')->getManager();

    $info = new Info();
    $info->setMail($request->request->get('mail'));
    $info->setName($request->request->get('name'));
    $info->setLastname($request->request->get('lastname'));

    $em->persist($info);

    $em->flush();

}

我正在考虑使用表/列/数据,如果一切正常,请在刷新后调用全局函数以将数据添加到更改表中。
是否有简单的方法/良好的做法来做到这一点?

I´m thinking about creating an object/array with the table/column/data and after the flush if everything is ok, call a global function to add the data into the changes table. Is there a easy way / good practice to do this?

推荐答案

首先,您不需要自己实现这个逻辑之王。原则在内部跟踪实体的插入,更新,字段更改和删除,并创建您可以收听的相应事件。例如,当您创建新的 Info 实体并将其持久化时,Doctrine将在调用<$ c时调度 prePersist 事件。 $ c> flush 操作。您可以创建事件订阅者来订阅此类事件,并在其中进行逻辑处理。

First of all you don't need to implement this king of logic yourself. Doctrine tracks entity insertions, updates, field changes and deletes internally, and creates corresponding events that you can listen to. For instance, when you create new Info entity and persist it, Doctrine will dispatch prePersist event when you call flush operation. You could create event subscriber to subscribe on such events and do your logic there.

要完全了解Doctrine生命周期事件,请查阅官方准则文档: http://docs.doctrine-project.org/projects/doctrine-orm /en/latest/reference/events.html

To fully understand Doctrine lifecycle events check out official doctrine documentation: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html

Symfony文档中描述的事件订户创建: http://symfony.com/doc/current/doctrine/event_listeners_subscribers.html#creating-the-subscriber- class

Event subscriber creation described in the Symfony documentation: http://symfony.com/doc/current/doctrine/event_listeners_subscribers.html#creating-the-subscriber-class

此外,您还可以尝试使用现有的第三方捆绑软件来记录实体的插入,更新和删除操作: https://github.com/simplethings/EntityAuditBundle 我还支持版本控制。

Also you can try out existing third-party bundle that will log entity insertions, updates and deletes: https://github.com/simplethings/EntityAuditBundle It also supports versioning.

这篇关于Symfony3将每个更新保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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