使用 Zend Framework 2,我正在尝试将 DBRecordExists 验证器添加到控制器中的表单。但是,我不断收到说“没有数据库适配器存在”的异常。我尝试了这个问题的答案,但它似乎没有用。有谁知道我做错了什么?
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\''),
),
),
),
),
),
);
模块.config.php
<?php
return array(
//other config code
'di' => array(
'Graduate\Controller\AuthController' => array(
'parameters' => array(
'adapter' => 'Zend\Db\Adapter\Adapter',
),
),
),
);