我有这个EventSubscriber:
class ChangeLogListener implements EventSubscriber
{
private $tokenStorage;
private $str,$str1;
public function __construct(TokenStorage $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function getSubscribedEvents()
{
return array(
'postPersist',
'postUpdate',
'onDelete',
);
}
public function postPersist(LifecycleEventArgs $args)
{
if (!$args->getEntity() instanceof ChangeLog)
$this->createLog($args, 'creation');
}
public function postUpdate(LifecycleEventArgs $args)
{
$this->createLog($args, 'update');
}
public function preRemove(LifecycleEventArgs $args)
{
$this->createLog($args, 'remove');
}
public function createLog(LifecycleEventArgs $args, $action)
{
# Entity manager
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
$entity = $args->getEntity();
# Get user
$user = $this->tokenStorage->getToken()->getUser();
#Get changes
$changes = $uow->getEntityChangeSet($entity);
$cl = new ChangeLog();
$cl->setDate(new \DateTime());
$cl->setUser($user);
$cl->setEntityName(get_class($entity));
$cl->setEntityId($entity->getId());
$cl->setAction($action);
$cl->setDescription($log);
$cl->setChangeset($changes);
$em->persist($cl);
$em->flush();
}
}
当我想要POST项目时,必须将一些数据记录到db。在我在db:
中的change_set中收到所有操作之后一个:3:{S:5: “值”;一个:2:{I:0; N; I:1; S:3: “120”;} S:4: “项目”;一个: 2:{I:0; N; I:1,O:21: “的appbundle \实体\物品”:6:{S:25: “的appbundle \实体\ ITEMID”; I:127; S:27:“的appbundle \实体\ ITEMNAME “; S:7:” 的newitem “; S:13:” *类别 “O:33:” 学说\ ORM \ PersistentCollection “:2:{S:13:” *集“,O:43 : “教义\共同\类别\ ArrayCollection的”:1:{S:53: “教义\共同\类别\ ArrayCollectionelements”;一个:2:{I 0,O:25: “的appbundle \实体\种类”:7 :{S:29: “的appbundle \实体\ CATEGORYID”; I:2; S:31: “的appbundle \实体\类别名称”; S:10:“子 至 1 “; S:33:” 的appbundle \实体\ Categoryparent “O:40:” Proxies__CG __ \的appbundle \实体\种类 “:8:{S:17:” 的将IsInitialized “; B:0 ; S:29: “的appbundle \实体\ CATEGORYID”; I:1; S:31: “的appbundle \实体\类别名称”; N; S:33: “的appbundle \实体\ Categoryparent”; N; S:35:”的appbundle \实体\ Categorychildren “; N; S:8:” *项 “; N; S:36:” 的appbundle \实体\ CategorycreatedAt “; N; S:36:” 的appbundle \实体\ CategoryupdatedAt“; N;} S; :35: “的appbundle \实体\ Categorychildren”,O:33: “学说\ ORM \ PersistentCollection”:2:{S:13: “*集”,O:43: “教义\共同\类别\ ArrayCollection的”:1 :{S:53: “教义\共同\类别\ ArrayCollectionelements”;一个:0:{}} S:14: “*初始化”; b:0;} S:8: “*项”,O:33: “学说\ ORM \ PersistentCollection”:2:{S:13: “*集”,O:43: “教义\共同\类别\ ArrayCollection的”:1:{S:53: “教义\共同\类别\ ArrayCollectionelements” ;一个:0:{}} S:14: “*初始化”; b:0;} S:36: “的appbundle \实体\ CategorycreatedAt”; N; S:36: “的appbundle \实体\ CategoryupdatedAt”; N; } I:1,O:25: “的appbundle \实体\种类”:7:{S :29: “的appbundle \实体\ CATEGORYID”; I:4; S:31: “的appbundle \实体\类别名称”; S:8: “child1.1”; S:33: “的appbundle \实体\ Categoryparent”; R :13; S:35: “的appbundle \实体\ Categorychildren”,O:33: “学说\ ORM \ PersistentCollection”:2:{S:13: “*集”,O:43:“教义\共同\类别\ ArrayCollection的 “:1:{S:53:” 教义\共同\类别\ ArrayCollectionelements “;一个:0:{}} S:14:” *初始化 “; b:0;} S:8:” *项“; ○:33: “学说\ ORM \ PersistentCollection”:2:{S:13: “*集”,O:43: “教义\共同\类别\ ArrayCollection的”:1:{S:53:“教义\共同\类别\ ArrayCollectionelements “;一个:0:{}} S:14:” *初始化 “; b:0;} S:36:” 的appbundle \实体\ CategorycreatedAt “; N; S:36:” 的appbundle \实体\ CategoryupdatedAt “; N;}}} S:14:” *初始化 “; b:1;} S:13:” *属性 “; N; S:32:” 的appbundle \实体\ ItemcreatedAt “O:8:” 日期时间“:3:{S:4:” 日期 “; S:26:” 2018年3月19日 10:22:47.000000 “; S:13:” timezone_type “; I:3; S:8:” 时区 “S:3:” UTC “;} S:32:” 的appbundle \实体\ ItemupdatedAt“; N; }} S:9: “属性”;一个:2:{I:0; N; I:1,O:26: “的appbundle \实体\属性”:3:{S:30:“的appbundle \实体\属性Id “; I:96; S:33:” 的appbundle \实体\ Attributealias “; S:5:” 价格 “; S:32:” 的appbundle \实体\ ATTRIBUTENAME “; S:5:” 价格“;}}} < / p>
但是我认为这个数据是不可读的。我认为我需要在将数据写入db之前对其进行解析,但我不明白如何将其解析为可读格式,如下所示:
名称:旧值:12 =&gt;新价值:121,更新时间:旧价值: 2018-03-20 05:51:44 =&gt;新价值:2018-03-20 08:36:12等
知道如何解析这个吗?
答案 0 :(得分:0)
您正在直接插入在具有整个对象的实体上完成的所有工作,这就是您将所有元数据保存到db中的原因。最好教授自定义扩展以处理此问题(doctrine-extensions并查看Loggable behavioral extension for Doctrine2),或者如果要创建自定义ChangeLogListner
,则使用方法计算或获取确切的更改 - 使用教义方法设置。方法see here。
你可以改变你的EventListner代码:
$em = $this->getDoctrine()->getManager();
$entity = $em->find('My\Entity', 1);
$entity->setTitle('Changed Title!');
$uow = $em->getUnitOfWork();
$uow->computeChangeSets(); // do not compute changes if inside a listener
$changeset = $uow->getEntityChangeSet($entity);
或检查Is there a built-in way to get all of the changed/updated fields in a Doctrine 2 entity
如果您在EventListner中尝试,请尝试在特定事件中进行:
public function preUpdate(Event\LifecycleEventArgs $eventArgs)
{
$changeArray = $eventArgs->getEntityChangeSet();
//do stuff with the change array
}