我在ZF2供應商庫中創建了一個排序基礎模塊。到目前爲止,所有事情都以我想要的方式工作。我確實有問題。雖然我能夠擴展基本模塊的控制器,但我無法訪問基本服務。我使用Doctrine 2作爲我的數據庫層。調用成員函數get()
實施服務定位之後,我得到致命錯誤:調用一個成員函數get()方法在我的基本服務文件中的非對象。我的BaseService文件顯示如下:
namespace MyResource\Service;
use Doctrine\ORM\Mapping as ORM;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class BaseService implements ServiceLocatorAwareInterface
{
/**
* Entity manager instance
*
* @var Doctrine\ORM\EntityManager
*/
protected $_em;
protected $_serviceLocator;
public function __construct()
{
$this->getEntityManager();
}
/**
* Returns an instance of the Doctrine entity manager loaded from the service
* locator
*
* @return Doctrine\ORM\EntityManager
*/
public function getEntityManager()
{
if (null === $this->_em) {
$this->_em = $this->getServiceLocator()
->get('doctrine.entitymanager.orm_default');
}
return $this->_em;
}
/**
* Set serviceManager instance
*
* @param ServiceLocatorInterface $serviceLocator
* @return void
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
}
/**
* Retrieve serviceManager instance
*
* @return ServiceLocatorInterface
*/
public function getServiceLocator()
{
return $this->serviceLocator;
}
}
任何人都可以幫忙嗎?
感謝
在使用它之前,您是否從ServiceManager調用/創建服務? – yechabbi