0

ZF2ベンダーライブラリにソートベースモジュールを作成しました。これまでのところ、すべてが私が望むように機能しています。私には問題があります。ベースモジュールのコントローラーを拡張することはできますが、ベースサービスにアクセスできません。データベースレイヤーとしてDoctrine2を使用しています。

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

使用してuse use Zend\ServiceManager\ServiceManagerAwareInterfaceいますが、実装していますServiceLocatorAwareInterface(そのための「使用」ステートメントはありません)。

于 2015-09-28T21:51:13.243 に答える
0

1):

あなたのプロパティは呼び出されます

protected $_serviceLocator;

しかし、あなたは自分の値をに割り当てています

protected $serviceLocator;

2)

DI またはサービス マネージャーを介してサービスを作成していますか? その場合、ServiceLocator は自動的に挿入されます。「new」キーワードを使用して手動で作成している場合、ServiceLocator は添付されません。

于 2013-02-08T09:38:35.207 に答える
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 に答える