Zend \ ServiceManager \ ServiceManager :: get无法获取或创建getAlbumTable的实例 [英] Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for getAlbumTable
问题描述
我正在尝试修改和复制自定义模块,我已经设置了所有数据库连接,但是在查看我的模块时出现错误,如下所示:
I am trying to modify and copy a custom module i have setup everything DB connection is but getting the error while going to view my module as follows:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for getAlbumTable
这是我的module.config文件:
Here is the my module.config file:
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
这是global.php中的数据库连接
And here is the database connection in global.php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=stickynotes;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
这是来自module.php的服务配置代码:
public function getServiceConfig()
{
return array(
'factories' => array(
'Album\Model\AlbumTable' => function($sm) {
$tableGateway = $sm->get('AlbumTableGateway');
$table = new AlbumTable($tableGateway);
return $table;
},
'AlbumTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
以下是获取相册的控制器:
<?php
namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AlbumController extends AbstractActionController
{
protected $_albumTable;
public function indexAction()
{
return new ViewModel(array(
'albums' => $this->getAlbumTable()->fetchAll(),
));
}
}
?>
这是数据库表的附件: 数据库表视图
Here is the attachment for the database tables: Database tables View
有人可以让我知道我必须在哪里调试此错误并解决问题吗?
Can anyone please let me know where i have to debug this error and fix the problem?
推荐答案
我已经通过在模块文件Module.php中添加服务来解决它,现在它可以正常工作:
I have Just Solved it by adding the Services in my Module file Module.php now the it is working fine:
public function getServiceConfig()
{
return array(
'factories' => array(
'Album\Model\AlbumTable' => function($sm) {
$tableGateway = $sm->get('AlbumTableGateway');
$table = new AlbumTable($tableGateway);
return $table;
},
'AlbumTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
这篇关于Zend \ ServiceManager \ ServiceManager :: get无法获取或创建getAlbumTable的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!