gpt4 book ai didi

controller - ZF2 - Controller 中的访问数据库适配器

转载 作者:行者123 更新时间:2023-12-04 05:09:09 27 4
gpt4 key购买 nike

使用 Zend Framework 2,我正在尝试将 DBRecordExists 验证器添加到 Controller 中的表单。但是,我不断收到说“没有数据库适配器存在”的异常。我尝试了 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\''),
),
),
),
),
),
);

模块.config.php
<?php
return array(
//other config code

'di' => array(
'Graduate\Controller\AuthController' => array(
'parameters' => array(
'adapter' => 'Zend\Db\Adapter\Adapter',
),
),
),
);

最佳答案

尝试此行以获取 Controller 中的当前 DB 适配器:

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

或者

引用这个链接,

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

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

我希望这会有所帮助。

关于controller - ZF2 - Controller 中的访问数据库适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15112281/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com