Iterating through Doctrine changeSet

I am trying to register certain actions that users take on my site and check the listener if some entities are validated, and if so my goal is to register the fields they are editing, but not all fields (some are not important or too long).

I have a problem saving a changeset to my database, so I want to filter important fields. This works for saving a changeset, but when there are multiple nested arrays in a changeset, the array is not saved correctly (it gets cut off after 3 or so arrays within the arrays). I am using 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 going through the docs but I'm not sure how to iterate over $ changeet. It is a multidimensional array that can contain a variable number of arrays depending on the number of fields updated. Userlog is a simple object I have to store $ changeet and the log field is an array.

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

+3


source to share


1 answer


I think I have a solution that works well. It adds an entity type, so it doesn't match the changeset exactly from Doctrine2, but I think it works for my purposes. I've found a bunch of other posts that form people trying to write specific changes to 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;
}

      

+1


source







All Articles