2017-03-04 143 views
0

我是新来的Zend Framework 3和我做这个教程: https://docs.zendframework.com/tutorials/getting-started/database-and-models/Zend框架3 XAMPP数据库连接

我有一个XAMPP,MySQL的安装。

我已经完成了本教程中的所有操作。现在我正在配置数据库连接。此外,我设置了控制器和视图。

在上面的教程链接,他们使用PHP来创建一个数据库,然后在配置/自动加载/ global.php .....以下代码:

return [ 
'db' => [ 
    'driver' => 'Pdo', 
    'dsn' => sprintf('sqlite:%s/data/zftutorial.db', realpath(getcwd())), 
    ], 
]; 

我已经编辑这个给:

'db' => [ 
'driver' => 'Pdo_Mysql', 
'dsn' => 'mysql:dbname=dbname;host=localhost;charset=utf8;username=myuser;password=mypassword', 
], 

当我调用用于索引视图的URL,存在以下错误:

Warning: Creating default object from empty value in C:\xampp\htdocs\zendtest\module\Album\src\Controller\AlbumController.php on line 15

Fatal error: Call to a member function fetchAll() on null in C:\xampp\htdocs\zendtest\module\Album\src\Controller\AlbumController.php on line 22

的AlbumController:

<?php 

    namespace Album\Controller; 

    use Album\Model\AlbumTable; 
    use Zend\Mvc\Controller\AbstractActionController; 
    use Zend\View\Model\ViewModel; 

    class AlbumController extends AbstractActionController 
    { 
     private $table; 
    public function __construct(AlbumTable $table) 
    { 
     $htis->table=$table; 
    } 


    public function indexAction() 
    { 
     return new ViewModel([ 
      'albums' => $this->table->fetchAll(), 
     ]); 
    }} 

我认为连接不起作用?

回答

0

你能分享你的“AlbumControllerFactory.php”吗?

如果你还没有创建你应该做的工厂。

1 - 创建AlbumControllerFactory实现FactoryInterface

2 - 里面__invoke功能使用容器注入AlbumTable到控制器

3 - 在module.config.php配置您的映射

'controllers' => [ 
    'factories' => [ 
     Controller\AlbumController::class => Controller\Factory\AlbumControllerFactory::class, 
0

所有的简单,你在关键$this错误,你确实写$htis而不是)

+1

这doe没有回答这个问题。一旦你有足够的[声誉](https://stackoverflow.com/help/whats-reputation),你将可以[对任何帖子发表评论](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提问者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [来自评论](/ review/low-quality-posts/17442930) – FluffyKitten