我正在尝试将用户实体添加到 preFlush 事件上的实体。基本上每个实体都有一个createdBy,它是一个用户实体。
每当我尝试这个时,我都会收到以下错误。
Notice: Undefined index: 0000000055f036b6000000009c9cc58f in /dir/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 870
这是我的 preFlush 函数。
public function preFlush(PreFlushEventArgs $args)
{
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
$securityContext = $this->container->get('security.context');
if($securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')
|| $securityContext->isGranted('IS_AUTHENTICATED_FULLY'))
{
$user = $securityContext->getToken()->getUser();
}
if(!$user)
{
$user = $em->getRepository('NAB\EnterpriseBundle\Entity\User')->findOneByUsername($this->username);
}
foreach ($uow->getScheduledEntityInsertions() as $entity)
{
$entity->setCreatedBy($user);
$em->persist($user);
}
$meta = $em->getClassMetadata(get_class($entity));
$uow->recomputeSingleEntityChangeSet($meta, $entity);
$uow->computeChangeSets($meta, $entity);
}
我究竟做错了什么?