ZF2 - get无法为getAlbumTable获取或创建实例

时间:2013-08-15 13:50:42

标签: zend-framework2

我在标题中使用了getAlbumTable,因为我面临的问题是基于Zend Album Tutorial,对于遇到类似问题的其他人来说可能更容易找到。我所做的就是将专辑重命名为“Champrallies”。

我的错误:

Zend\Mvc\Controller\PluginManager::get was unable to fetch or create an instance for getChampralliesTable

我要做的是从我创建的第二个模型中执行一个函数。我可以从'原始'模型执行没问题AlbumTable或在我的情况下执行ChampionshipTable。我正在尝试从第二个表中获取数据,但是在同一个模块中。第二个表称为“champ_rallies”,模型文件为Champrallies.php和ChampralliesTable.php。

在我的控制器中更改此部分

'champRallies' => $this->getChampralliesTable()->fetchAll(),

'champRallies' => $this->getChampionshipTable()->fetchAll(),

表示错误消息消失。所以我的第一个想法是命名空间或module.php有问题。 (原谅我,我对这一切都很新)

Module.php

<?php
namespace Championship;

use Championship\Model\Championship;
use Championship\Model\ChampionshipTable;
use Championship\Model\Champrallies;
use Championship\Model\ChampralliesTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

class Module
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    // Add this method:
    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Championship\Model\ChampionshipTable' =>  function($sm) {
                    $tableGateway = $sm->get('ChampionshipTableGateway');
                    $table = new ChampionshipTable($tableGateway);
                    return $table;
                },
                'ChampionshipTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Championship());
                    return new TableGateway('championships', $dbAdapter, null, $resultSetPrototype);
                },
                'Championship\Model\ChampralliesTable' =>  function($sm) {
                    $tableGateway = $sm->get('ChampralliesTableGateway');
                    $table = new ChampralliesTable($tableGateway);
                    return $table;
                },
                'ChampralliesTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Champrallies());
                    return new TableGateway('champ_rallies', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }
}

我的第二个想法是,也许我没有声明正确的命名空间或类似的东西。

ChampralliesTable.php

<?php

namespace Championship\Model;

use Zend\Db\TableGateway\TableGateway;

class ChampralliesTable

Champrallies.php

<?php

namespace Championship\Model;

class Champrallies

我担心我会忽略一些失败但我没有通过谷歌或在这里发现任何类似的帖子,任何帮助表示赞赏!

编辑:我忘了将getChampralliesTable函数添加到控制器本身,

public function getChampralliesTable()
{
This is Line 50 -> if (!$this->champralliesTable) {
        $sm = $this->getServiceLocator();
        $this->champralliesTable = $sm->get('Championship\Model\ChampralliesTable');
    }
    return $this->champralliesTable;
}

但现在我明白了,

Notice: Undefined property: Championship\Controller\ChampionshipController::$champralliesTable in /usr/home/phil/git/rallystats/module/Championship/src/Championship/Controller/ChampionshipController.php on line 50

1 个答案:

答案 0 :(得分:3)

我忘了将getChampralliesTable函数添加到控制器本身,

public function getChampralliesTable()
{
This is Line 50 -> if (!$this->champralliesTable) {
        $sm = $this->getServiceLocator();
        $this->champralliesTable = $sm->get('Championship\Model\ChampralliesTable');
    }
    return $this->champralliesTable;
}

但现在我明白了,

Notice: Undefined property: Championship\Controller\ChampionshipController::$champralliesTable in /usr/home/phil/git/rallystats/module/Championship/src/Championship/Controller/ChampionshipController.php on line 50

解决这个问题只需添加

protected $champralliesTable;

到我的ChampionshipController的顶部。

编辑:执行上述操作可以解决我的问题。