2017-03-04 144 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