我在将 EntityManger 注入 Symfony2 树枝扩展时遇到问题。我做了以下事情:
我注册了一个新的树枝扩展并创建了一个类。所有这些都在工作。现在我想访问数据库并尝试注入学说 EntityManager。我的第一步是将 EM 作为参数添加到构造函数中:
use XYZ\BlubBundle\Utils\SessionHelper;
class SessionHelperExtension extends \Twig_Extension
{
private $em;
private $conn;
protected $sessionHelper;
public function __construct(\Doctrine\ORM\EntityManager $em, \XYZ\BlubBundle\Utils\SessionHelper $sessionHelper) {
$this->em = $em;
$this->conn = $em->getConnection();
$this->sessionHelper = $sessionHelper;
}
[...]
}
?>
然后我在 service.yaml 中添加了以下几行:
twig.extension.sessionHelper:
class: XYZ\BlubBundle\Extensions\SessionHelperExtension
arguments: [@session_helper, @doctrine.orm.entity_manager]
tags:
- { name: twig.extension }
我自己的服务“sessionHelper”被注入没有问题,但 EntityManager 始终为“null”。我在这里做错了什么?
编辑:我得到以下异常:
Catchable Fatal Error: Argument 2 passed to
XYZ\BlubBundle\Extensions\SessionHelperExtension::__construct() must be an instance of
Doctrine\ORM\EntityManager, none given,
called in /.../Symfony/app/cache/dev/appDevDebugProjectContainer.php on line 2918 and
defined in /.../Symfony/src/XYZ/BlubBundle/Extensions/SessionHelperExtension.php line 12