使用Zend Framework 2,我試圖在控制器中添加一個DBRecordExists驗證器。但是,我不斷地收到說「沒有數據庫適配器」的異常。我嘗試了this question的答案,但它似乎沒有工作。有誰知道我做錯了什麼?ZF2 - 訪問控制器中的數據庫適配器
AuthController.php
use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\AdapterAwareInterface;
class AuthController extends AbstractActionController implements AdapterAwareInterface
{
/**
* @var Zend\Db\Adapter\Adapter
*/
protected $adapter;
/**
* Input email address to retrieve a lost password.
*/
public function lostpasswordAction()
{
// Create form
$form = new UserForm();
// Perform validation
$request = $this->getRequest();
if ($request->isPost()) {
$user = new User();
$form->setInputFilter($user->getInputFilter());
$form->setData($request->getPost());
$recordValidator = new RecordExists(
array(
'table' => 'users',
'field' => 'email'
));
$form->getInputFilter()->get('email')->getValidatorChain()->addValidator($recordValidator);
//code
}
}
public function setDbAdapter(Adapter $adapter)
{
$this->adapter = $adapter;
}
}
local.config.php
<?php
return array(
'di' => array(
'instance' => array(
'Zend\Db\Adapter\Adapter' => array(
'parameters' => array(
'driver' => 'Zend\Db\Adapter\Driver\Pdo\Pdo',
),
),
'Zend\Db\Adapter\Driver\Pdo\Pdo' => array(
'parameters' => array(
'connection' => 'Zend\Db\Adapter\Driver\Pdo\Connection',
),
),
'Zend\Db\Adapter\Driver\Pdo\Connection' => array(
'parameters' => array(
'connectionInfo' => array(
'dsn' => "mysql:dbname=owp;host=localhost",
'username' => 'redacted',
'password' => 'redacted',
'driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''),
),
),
),
),
),
);
module.config.php
<?php
return array(
//other config code
'di' => array(
'Graduate\Controller\AuthController' => array(
'parameters' => array(
'adapter' => 'Zend\Db\Adapter\Adapter',
),
),
),
);
我嘗試了這兩種解決方案。第一個解決方案說$ this-> adapter是null。第二種解決方案確實改變了情況,但是這次它引發了另一個異常:「array_key_exists()期望參數2是數組,給定爲null」,在AbstractDb中調用125,在401中由Di調用。 – Lex 2013-02-27 13:55:21