通过主义的迭代变更 [英] iterating through Doctrine's changeSet

查看:202
本文介绍了通过主义的迭代变更的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图登录用户正在我的网站上的特定操作,并有一个侦听检查是否正在更新某些实体,以及如果是的话,我的目标是登录他们正在编辑的字段,但不是所有的字段(一些不重要或太长)。

I am attempting to log specific actions users are taking on my site and have a listener check if certain entities are being updated, and if so, my goal is to log the fields they are editing, but not all the fields (some are not important or too long).

我保存变更到我的数据库这就是为什么我希望筛选重要领域的问题。本工程以保存变更,但是当有变更集内的几个嵌套的数组,数组是不正确保存(它切断后3左右阵列阵列内)。我现在用在Postgres数组类型。在我postupdate事件我有:

I have a problem saving the changeset to my database which is why I want to filter for important fields. This works to save the changeset, but when there are several nested arrays within the changeset, the array is not saved correctly (it cuts off after 3 or so arrays within arrays). I am using the array type in postgres. In my postupdate event I have:

if ($entity instanceof ListingQuery) {
        $entityManager = $eventArgs->getEntityManager();
        $ul = new UserLog();
        $uow = $entityManager->getUnitOfWork();
        $changeset = $uow->getEntityChangeSet($entity);
        $ul = new UserLog();
        $ul->setLog($changeset);
        $ul->setUser($entity->getUser());
        $entityManager->persist($ul);
        $entityManager->flush();
    }

我一直在找过的文档,但我真的不知道如何在$变更迭代。它是一个多维阵列中基于更新字段的数量,可以具有阵列的变量数量。用户日志是一个简单的实体我有节省$变更和日志字段是一个数组。

I've been looking over the docs, but am not really sure how to iterate over the $changeset. It's a multidimension array that can have a variable amount of arrays within based on the number of fields updated. Userlog is a simple entity I have for saving the $changeset and the log field is an array.

我创建了一个函数,是以$变更,并通过第一三级阵列的循环,但其不保存字段的名称和仅前后保存的值。如何访问在$变更改变了字段名?

I created a function that takes the $changeset and loops through the first three levels of the array, but its not saving the name of the field and only saves the values before and after. How do I access the field names changed in the $changeset?

推荐答案

我觉得我有一个行之有效的解决方案。所以它不准确变更从Doctrine2匹配,但我觉得对我的作品的目的是增加了实体类型。我发现了一堆其他职位的形式人们试图登录主义,结果好坏参半的具体变化,所以请后,如果其他人有更好的解决方案。

I think I have a solution that works well. It adds the entity type so it does not match the changeset exactly from Doctrine2, but I think works for my purpose. I found a bunch of other posts form people trying to log specific changes in Doctrine with mixed results so please post if anyone else has a better solution.

public function looparray($arr, $type) {
    $recordset[] = array($type);
    $keys[] = array_keys($arr);
    foreach ($keys as $key) {
        if (!is_array($key)) {
            if (array_key_exists($key, $arr)) {
                $recordset[] = array($key => $arr[$key]);
            }
        } else {
            foreach ($key as $key1) {
                if (!is_array([$key1])) {
                    $recordset[] = array($key1 => $arr[$key1]);
                } else {
                    if (!is_array([$key1])) {
                        $recordset[] = array($key1 => $arr[$key1]);
                    } else {
                        $recordset[] = array($key1 . ' changed ' => $key1);
                    }
                }
            }
        }
    }
    return $recordset;
}

这篇关于通过主义的迭代变更的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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