我試圖讓一個特定的模塊連接到不同的數據庫。 [我已經問] [1]如何做到這一點,並提供了[此鏈接] [2]。zend 2:試圖通過'適配器'鍵在local.php中指定備用數據庫
它不是非常複雜,但它看起來像我做錯了,因爲我看到一個ServiceNotCreatedException。
這裏是我的配置/自動加載/ local.php的樣子:
return array(
'db' => array(
//Override global.php 'dsn' to specify local port on dev box
'dsn' => 'mysql:dbname=zf2tutorial;host=localhost;port=3307',
'username' => 'root',
'password' => 'pass!',
'adapters' => array(
'testdb' => array(
'dsn' => 'mysql:dbname=otherdb;host=testsrv',
'username' => 'root',
'password' => 'pass',
)
)
),
);
Now here is what I have the module's Module.php:
public function getServiceConfig() {
return array(
'factories' => array(
'Helpdesk\Model\TicketTable' => function($sm) {
$tableGateway = $sm->get('TicketTableGateway');
$table = new TicketTable($tableGateway);
return $table;
},
'TicketTableGateway' => function($sm) {
$dbAdapter = $sm->get('testdb');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(
new Ticket());
return new TableGateway('ticket', $dbAdapter, null,
$resultSetPrototype);
},
),
);
}
這裏是控制器的方法:
namespace Helpdesk\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Helpdesk\Model\Ticket;
class HelpdeskController extends AbstractActionController {
protected $ticketTable;
public function indexAction() {
return new ViewModel(array(
'tickets' => $this->getTicketTable()->fetchAll()
));
}
我使用的是TableGateway以同樣的方式[zend教程] [3]。 我這樣做:
$dbAdapter = $sm->get('testdb');
取而代之的是:
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
我誤解這個工程?
編輯: 這裏是堆棧跟蹤:
An exception was raised while creating "Helpdesk\Model\TicketTable"; no instance returned
App\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
App\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
[internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
App\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
App\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
App\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
App\public\index.php(17): Zend\Mvc\Application->run()
{main}
該錯誤涉及'Helpdesk \ Model \ TicketTable',它不在您發佈的Module.php代碼中 - 您是否可以編輯您的問題以包含該問題?我也不會混合和匹配數據庫配置的語法,所以將一個單獨的條目添加到適配器數組中,然後將您的zf2tutorial移動到該數組中。 –
嗯,根據文章鏈接,這傢伙建議這樣做來指定一個主\默認,正常的適配器類將拿起,並具有必須明確提到的'適配器'鍵下的輔助適配器。我還發現我有一些混淆問題的名稱空間問題。很快我會重新編輯帖子,使其更清晰。 – red888
是的,對不起,你說得很對,我只是重新閱讀文章。所以你擁有它的方式很好。 –