如何跟踪Eventlistener中的更改并将其写入DB?

时间:2018-03-18 15:52:52

标签: php symfony doctrine-orm

我正在尝试监控我的应用中发生的情况。为此我创建了这个类:

class ChangeLogListener implements EventSubscriber
{
    private $tokenStorage;

    public function __construct(TokenStorage $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }

    public function getSubscribedEvents()
    {
        return array(
            'postPersist',
            'postUpdate',
            'onDelete',
        );
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        # Avoid to log the logging process
        if (!$args->getEntity() instanceof ChangeLog)

            $this->createLog($args, 'creation');
    }

    public function postUpdate(LifecycleEventArgs $args)
    {

        $this->createLog($args, 'update');
    }

    public function preRemove(LifecycleEventArgs $args)
    {
        # Handle the log creation
        $this->createLog($args, 'remove');
    }

    public function createLog(LifecycleEventArgs $args, $action)
    {
        $em = $args->getEntityManager();
        $uow = $em->getUnitOfWork();
        $entity = $args->getEntity();

        $user = $this->tokenStorage->getToken()->getUser();

        $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->setChangeset($changes);//<---change this 
        $cl->setDescription('');

        $em->persist($cl);
        $em->flush();
    }
}

但是我对更改有一些问题,我不知道如何正确地将它们写入数据库并使它们可读。也许有一些方法可以正确地做到这一点?
当我尝试刷新更改时,我总是会收到错误:

  

字符串数据,右截断:1406列的数据太长   &#39; change_set&#39;在第1行&#34;

My ChangeLog.orm.yml:

#...
fields:
   changeSet:
         type: array
#... 

1 个答案:

答案 0 :(得分:0)

var_dump($changes);die();行之后执行getEntityChangeSet(),并从该数据集中找出您真正想要的内容。对于你试图保存它的领域来说,它目前太长了。它需要减少。