Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Zend framework2 ZF2-get无法获取或创建getAlbumTable的实例_Zend Framework2 - Fatal编程技术网

Zend framework2 ZF2-get无法获取或创建getAlbumTable的实例

Zend framework2 ZF2-get无法获取或创建getAlbumTable的实例,zend-framework2,Zend Framework2,我在标题中使用了getAlbumTable,因为我面临的问题是基于Zend Album教程的,其他有类似问题的人可能更容易找到。我所做的就是把这张专辑改名为“冠军杯” 我的错误: Zend\Mvc\Controller\PluginManager::get was unable to fetch or create an instance for getChampralliesTable 我要做的是从我创建的第二个模型执行一个函数。我可以从“原始”模型无问题AlbumTable或Champio

我在标题中使用了getAlbumTable,因为我面临的问题是基于Zend Album教程的,其他有类似问题的人可能更容易找到。我所做的就是把这张专辑改名为“冠军杯”

我的错误:

Zend\Mvc\Controller\PluginManager::get was unable to fetch or create an instance for getChampralliesTable
我要做的是从我创建的第二个模型执行一个函数。我可以从“原始”模型无问题AlbumTable或ChampionshipTable执行。我试图从第二个表中获取数据,但在同一个模块中。第二个表称为“champ_ralles”,模型文件为champralles.php和ChampralliesTable.php

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

'champRallies' => $this->getChampralliesTable()->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);
                },
            ),
        );
    }
}
但现在我明白了

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

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

public function getChampralliesTable()
{
This is Line 50 -> if (!$this->champralliesTable) {
        $sm = $this->getServiceLocator();
        $this->champralliesTable = $sm->get('Championship\Model\ChampralliesTable');
    }
    return $this->champralliesTable;
}
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
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;
到我的冠军之巅


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

只需将属性添加到控制器:protected$champralliesTable中即可
protected $champralliesTable;