Zend framework2 对成员函数get()的调用

Zend framework2 对成员函数get()的调用,zend-framework2,Zend Framework2,我在ZF2供应商库中创建了一个排序基模块。到目前为止,一切都按照我希望的方式进行。我确实有个问题。虽然我能够扩展基本模块的控制器,但我无法访问基本服务。我使用条令2作为我的数据库层 在实现ServiceLocator之后,我遇到了一个致命错误:对我的基本服务文件中的非对象调用成员函数get()。我的BaseService文件如下所示: namespace MyResource\Service; use Doctrine\ORM\Mapping as ORM; use Zend\ServiceM

我在ZF2供应商库中创建了一个排序基模块。到目前为止,一切都按照我希望的方式进行。我确实有个问题。虽然我能够扩展基本模块的控制器,但我无法访问基本服务。我使用条令2作为我的数据库层

在实现ServiceLocator之后,我遇到了一个致命错误:对我的基本服务文件中的非对象调用成员函数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;
    }

}
有人能帮忙吗

谢谢

1):

你的财产被称为

protected $_serviceLocator;
但是您正在将您的值分配给

protected $serviceLocator;
(二)

您是通过DI还是服务经理创建服务?如果您这样做,那么ServiceLocator应该自动为您注入,如果您使用“new”关键字手动创建它,那么它将不会附加ServiceLocator

ZF2中似乎存在故障。如果您尝试设置属性 如下所示,问题将得到解决。像这样试试

忽略这一点

我希望这能帮你节省时间


您使用的是
use-use-Zend\ServiceManager\ServiceManagerAwareInterface
,但您正在实现
ServiceLocatorAwareInterface
(该接口没有“use”语句)。

在使用服务管理器之前,您是否从服务管理器调用/创建服务?
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;
    }
  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;
                }