以下代码在 AbstractPlugin 中运行良好,但我需要在视图助手中访问实体管理器。如何在视图助手中获取实体管理器?
有没有办法将 entityManager 实例保存在注册表中的某个位置,以便我可以随时随地访问它?那会是一个好习惯吗?
use Zend\View\Helper\AbstractHelper;
use Doctrine\ORM\EntityManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
class IsAuthz extends AbstractHelper implements ServiceManagerAwareInterface
{
/*
* @var Doctrine\ORM\EntityManager
*/
protected $em;
protected $sm;
public function __construct($e) {
$app = $e->getParam('application');
$em = $this->getEntityManager();
}
public function __invoke()
{
return $this;
}
/**
* @return Doctrine\ORM\EntityManager
*/
public function getEntityManager()
{
if (null === $this->em) {
$this->em = $this->sm->getServiceLocator()->get('doctrine.entitymanager.orm_default');
}
return $this->em;
}
/**
*
* @param \Doctrine\ORM\EntityManager $em
*/
public function setEntityManager(EntityManager $em)
{
$this->em = $em;
}
/**
* Retrieve service manager instance
*
* @return ServiceManager
*/
public function getServiceManager()
{
return $this->sm->getServiceLocator();
}
/**
* Set service manager instance
*
* @param ServiceManager $locator
* @return void
*/
public function setServiceManager(ServiceManager $serviceManager)
{
$this->sm = $serviceManager;
}
}
错误:
Fatal error: Call to a member function getServiceLocator() on a non-object in XXX/XXX/src/XXX/View/Helper/IsAuthz.php on line 41