0

我在我的 ZF2 供应商库中创建了一个排序基础模块。到目前为止,一切都按我希望的方式工作。我确实有问题。虽然我能够扩展基本模块的控制器,但我无法访问基本服务。我使用 Doctrine 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;
    }

}

任何人都可以帮忙吗?

谢谢

4

3 回答 3

0

ZF2 中似乎有一个小故障。如果您尝试如下设置属性,问题将得到解决。像这样试试

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

我希望这可以帮助您节省时间。

于 2013-08-09T15:46:34.273 回答
0

1):

你的财产被称为

protected $_serviceLocator;

但是您正在将您的值分配给

protected $serviceLocator;

2)

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

于 2013-02-08T09:38:35.207 回答
0

您正在使用use use Zend\ServiceManager\ServiceManagerAwareInterface但您正在实施ServiceLocatorAwareInterface(并且没有“使用”声明)。

于 2015-09-28T21:51:13.243 回答