ZF2 - 控制器中的Access DB Adapter

时间:2013-02-27 12:35:15

标签: controller zend-framework2 adapter validation

使用Zend Framework 2,我试图将DBRecordExists验证器添加到控制器中的表单。但是,我不断得到说“没有数据库适配器”的异常。我尝试了this question的答案,但似乎没有效果。有谁知道我做错了什么?

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',
                ),
            ),
    ),
);

2 个答案:

答案 0 :(得分:3)

尝试使用此行获取控制器中的当前数据库适配器:

 $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter'); 

参考此链接,

https://stackoverflow.com/a/17942232/2190889

借助此链接,您可以在Application / Module.php中设置数据库适配器,并且可以在系统中的任何位置使用。

我希望这有帮助。

答案 1 :(得分:0)

问题是您的RecordExists验证程序需要访问数据库适配器。

控制器:

$recordValidator = new RecordExists(array(
    'table' => 'users',
    'field' => 'email'
));
// Need to pass to the validator..
$recordValidator->setAdapter($this->adapter);

显然最好使用DI实例化Recr​​odExists验证器,并自动注入数据库适配器

DI配置:

'Zend\Validator\Db\RecordExists' => array(
     'parameters' => array(
          'adapter'  => 'Zend\Db\Adapter\Adapter',
      ),
 ),