我尝试设置服务以从我的数据库中检索实体.我的services.yml看起来像这样:
app.twig_global_extension:
class: AppBundle\Twig\GlobalExtension
arguments: [ "@doctrine.orm.entity_manager", "@security.token_storage" ]
tags:
- { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)
我的GlobalExtension是这样的:
<?php
namespace AppBundle\Twig;
class GlobalExtension extends \Twig_Extension
{
protected $em;
protected $token_storage;
public function __construct(\Doctrine\ORM\EntityManager $em, \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage $token_storage)
{
$this->em = $em;
$this->token_storage = $token_storage;
}
public function getGlobals()
{
return [
'userdata' => $this->em->getRepository('AppBundle:userdata')->findOneBy(['internalid' => $this->token_storage->getToken()->getUser()->getId()]),
];
}
public function getName()
{
return 'app_global_extension';
}
}
Run Code Online (Sandbox Code Playgroud)
它检索正确的实体,但我的Profiler在页面加载完成后抛出错误:
BadMethodCallException in GlobalExtension.php line 18:
Call to a member function getUser() on a non-object (null)
Run Code Online (Sandbox Code Playgroud)
首先,在这种情况下问题是什么? …