4

我在标题中使用了 getAlbumTable,因为我面临的问题是基于 Zend Album 教程,对于其他有类似问题的人来说可能更容易找到。我所做的只是将专辑重命名为“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有问题。(原谅我,我对这一切都很陌生)

模块.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
4

1 回答 1

4

我忘记将 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 的顶部。

编辑:做上述解决了我的问题。

于 2013-08-15T23:19:43.233 回答