2012-11-02 26 views
1

使用ZF1創建數據庫連接字符串時,它沒有任何問題。但在ZF2數據庫適配器似乎BUG永遠不會奏效,它給一直執行以下操作:未找到使用命名空間的類

錯誤:

[Fri Nov 02 13:26:25 2012] [error] [client 127.0.0.1] PHP Fatal error: Class 'Application\\Controller\\Zend\\Db\\Adapter\\Adapter' not found in /var/www/html/org/module/Application/src/Application/Controller/TestController.php on line 17 

代碼:

<?php 
namespace Application\Controller; 
use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use Zend\Db\Adapter\Adapter; 
//use Zend\Db\Adapter\Platform\PlatformInterface; 
//use Zend\Db\ResultSet\ResultSet; 
class TestController extends AbstractActionController { 
    public function indexAction() { 
    $driverArray = array(
    'driver' => 'Pdo_Mysql', 
    'database' => 'web145', 
    'username' => 'web145', 
    'password' => 'meandmypassword', 
    'host'=>'localhost' 
    ); 
    $adapter = new Zend\Db\Adapter\Adapter($driverArray); 
    //Zend\Db\Adapter\Adapter::query('SELECT * FROM `sh_operaor`'); 
    //var_dump ($adapter->query('SELECT * FROM `sh_operaor`')); 
    exit; 
    } 
} 

回答

3

既然你已經使用了命名空間的Zend \ DB \適配器\適配器,你應該這樣稱呼它:

$adapter = new Adapter($driverArray); 

或者你可以從根命名空間啓動它的啓動帶反斜槓

$adapter = new \Zend\Db\Adapter\Adapter($driverArray); 
+1

而且,如果要使用完全限定名稱,則必須以反斜槓\開始。如果沒有,PHP會在你當前的命名空間中尋找一個子命名空間'Zend'。 – Carlos

+1

您在完全限定名稱示例中添加了一個額外的'Adapter'。 – Carlos

+0

修好了,謝謝 – AlloVince