ZF2 - get was unable to fetch or create an instance for getAlbumTable
zend-framework2
Solution
I had forgotten to add getChampralliesTable function to the controller itself,
public function getChampralliesTable()
{
This is Line 50 -> if (!$this->champralliesTable) {
$sm = $this->getServiceLocator();
$this->champralliesTable = $sm->get('Championship\Model\ChampralliesTable');
}
return $this->champralliesTable;
}
But now I'm getting this,
Notice: Undefined property: Championship\Controller\ChampionshipController::$champralliesTable in /usr/home/phil/git/rallystats/module/Championship/src/Championship/Controller/ChampionshipController.php on line 50
Solving this was just a matter of adding
protected $champralliesTable;
to the top of my ChampionshipController.
edit: Doing the above solves my problem.
Problem
I used the getAlbumTable in the title since the problem I'm facing is based on the Zend Album Tutorial, might be easier for others having a similar problem to find. All I've done is rename Album to "Champrallies". My error: ``` Zend\Mvc\Controller\PluginManager::get was unable to fetch or create an instance for getChampralliesTable ``` What I'm trying to do is execute a function from a second Model I've created. I can execute from the 'original' model no problem AlbumTable or in my case ChampionshipTable. I'm trying to get data from a second table, but within the same module. The second table is called "champ_rallies" and the Model files are Champrallies.php and ChampralliesTable.php. Changing this part in my controller ``` 'champRallies' => $this->getChampralliesTable()->fetchAll(), ``` to ``` 'champRallies' => $this->getChampionshipTable()->fetchAll(), ``` means the error message disappears. So my first thought is that there is something wrong with namespaces, or module.php. (Forgive me, I'm fairly new at all this) 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); }, ), ); } } ``` My second thought is that perhaps I'm not declaring the right namespace, or something similar. ChampralliesTable.php ``` <?php namespace Championship\Model; use Zend\Db\TableGateway\TableGateway; class ChampralliesTable ``` Champrallies.php ``` <?php namespace Championship\Model; class Champrallies ``` I fear I'm overlooking something failure but I haven't found any similar posts through google or on here, any help is appreciated! edit: I had forgotten to add getChampralliesTable function to the controller itself, ``` public function getChampralliesTable() { This is Line 50 -> if (!$this->champralliesTable) { $sm = $this->getServiceLocator(); $this->champralliesTable = $sm->get('Championship\Model\ChampralliesTable'); } return $this->champralliesTable; } ``` But now I'm getting this, ``` Notice: Undefined property: Championship\Controller\ChampionshipController::$champralliesTable in /usr/home/phil/git/rallystats/module/Championship/src/Championship/Controller/ChampionshipController.php on line 50 ```