我正在尝试监视我的应用程序中发生的情况。为此,我创建了这个类:
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 数据对于列来说太长 第 1 行的"change_set">
My ChangeLog.orm.yml:
#...
fields:
changeSet:
type: array
#...
在getEntityChangeSet()
行之后做一个var_dump($changes);die();
,弄清楚你真正想要从该数据集中得到什么。对于您尝试保存它的字段来说,它目前太长了。它需要减少。