Change field field changes in a Doctrine object

I want to track changes in the Entity Entity field. I am using Symfony 2.5.0 and Doctrine 2.2.3.

So far I have EventSubscriberone that subscribes to preUpdate. Here I want to create a new Entity that saves the new and old value and contains a link to the updated Entity.

The problem is that I cannot find a way to save this new Entity. If I'm persist()in preUpdateand flush()in postUpdate, it works if I change only one entity. If several objects are changed, I get an error that the set of changes is empty.

I tried to play different events with different results. Blank pages, tracking elements are not saved, etc.

I think this should be a common case, but I can not find examples.

+4
source share
2 answers

Do not use preUpdate or postUpdate, you will have problems. Take a look at OnFlush instead.

At this point, you have access to the complete set of changes so that you can find out which fields have been changed, what has been added, etc. You can also safely save new objects. Note, as the documentation says , you will have to recount the change sets when you save or modify the objects.

The simple example that I put together did not test, but something similar to this will give you what you want.

public function onFlush(OnFlushEventArgs $args) {

    $entityManager = $args->getEntityManager();
    $unitOfWork = $entityManager->getUnitOfWork();
    $updatedEntities = $unitOfWork->getScheduledEntityUpdates();

    foreach ($updatedEntities as $updatedEntity) {

        if ($updatedEntity instanceof YourEntity) {

            $changeset = $unitOfWork->getEntityChangeSet($updatedEntity);

            if (!is_array($changeset)) {

                return null;
            }

            if (array_key_exists('someFieldInYourEntity', $changeset)) {

                $changes = $changeset['someFieldInYourEntity'];

                $previousValueForField = array_key_exists(0, $changes) ? $changes[0] : null;
                $newValueForField = array_key_exists(1, $changes) ? $changes[1] : null;

                if ($previousValueForField != $newValueForField) {

                    $yourChangeTrackingEntity = new YourChangeTrackingEntity();
                    $yourChangeTrackingEntity->setSomeFieldChanged($previousValueForField);
                    $yourChangeTrackingEntity->setSomeFieldChangedTo($newValueForField);

                    $entityManager->persist($yourChangeTrackingEntity);
                    $metaData = $entityManager->getClassMetadata('YourNameSpace\YourBundle\Entity\YourChangeTrackingEntity');
                    $unitOfWork->computeChangeSet($metaData, $yourChangeTrackingEntity);
                }
            }
        }
    }
}
+7
source

EntityAudit.

, . . , .

, :

$revisions = $auditReader->findRevisions('AppBundle\Entity\Article', 1);

:

$oldArticle = $auditReader->find(
  'AppBundle\Entity\Article',
  $id = 1,
  $rev = 2
);

.

, , , .

comparison choice

comparison

+2

Source: https://habr.com/ru/post/1599096/


All Articles