2

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

2 回答 2

3

Try this line to get current DB adapter in controller:

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

or

Refer this link,

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

With the help of this link you can set the DB Adapter in a Application/Module.php and you can use anywhere in your system.

I hope this help.

于 2013-07-30T08:53:52.063 回答
0

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

控制器:

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

显然,最好使用 DI 实例化您的 RecrodExists 验证器,并自动注入 DB Adapter

DI配置:

'Zend\Validator\Db\RecordExists' => array(
     'parameters' => array(
          'adapter'  => 'Zend\Db\Adapter\Adapter',
      ),
 ),
于 2013-02-27T13:18:52.410 回答