2013-01-31 46 views
0

我在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; 
    } 

} 

任何人都可以幫忙嗎?

感謝

+0

在使用它之前,您是否從ServiceManager調用/創建服務? – yechabbi

回答

0

1):

你的屬性被稱爲

protected $_serviceLocator; 

但你是你創建分配的值

protected $serviceLocator; 

2)

你的通過DI或服務經理提供服務?如果你這樣做,那麼應該爲你自動注入ServiceLocator,如果你使用「new」關鍵字手動創建它,那麼它將不會附加ServiceLocatior。

0

There seems to be a glitch in ZF2 .If you try setting the properties as below the problem will be fixed. Try like this

foreach ($resultSet as $row) { 

     $entity = new Countrypages(); 
      $entity->setId($row->id); 
     $entity->setName($row->name); 
     $entity->setSnippet($row->snippet); 
     $entity->setSortorder($row->sortorder); 
     $entity->setActive($row->active); 
     $entity->setCreated($row->created); 
     $entity->setModified($row->modified); 
     $entity->setFirstname($row->firstname); 
     $entity->setCreatedby($row->createdby); 
     $entities[] = $entity; 
    } 

ignore this

foreach ($resultSet as $row) { 

        $entity = new Countrypages(); 
        $entity->setId($row->id); 
        ->setName($row->name); 
        ->setSnippet($row->snippet); 
        ->setSortorder($row->sortorder); 
        ->setActive($row->active); 
        ->setCreated($row->created); 
        ->setModified($row->modified); 
        ->setFirstname($row->firstname); 
        ->setCreatedby($row->createdby); 
        $entities[] = $entity; 
       } 

I hope this help you save your time.

-1

您使用use use Zend\ServiceManager\ServiceManagerAwareInterface但你實現ServiceLocatorAwareInterface(而且也沒有 「使用」 爲一個語句)。