我已按照以下方式配置dbadapters。
在config / autoload / global.php
中添加了以下代码 use Zend\Session\Storage\SessionArrayStorage;
use Zend\Session\Validator\RemoteAddr;
use Zend\Session\Validator\HttpUserAgent;
use Zend\Db\Adapter;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
// Session configuration.
'session_config' => [
'cookie_lifetime' => 60 * 60 * 1, // Session cookie will expire in 1 hour.
'gc_maxlifetime' => 60 * 60 * 24 * 30, // How long to store session data on server (for 1 month).
],
// Session manager configuration.
'session_manager' => [
// Session validators (used for security).
'validators' => [
RemoteAddr::class,
HttpUserAgent::class,
]
],
// Session storage configuration.
'session_storage' => [
'type' => SessionArrayStorage::class
],
'service_manager' => [
'abstract_factories' => [
Adapter\AdapterAbstractServiceFactory::class,
],
'factories' => [
Adapter\AdapterInterface::class => Adapter\AdapterServiceFactory::class,
],
'aliases' => [
Adapter\Adapter::class => Adapter\AdapterInterface::class,
],
],
];
之后我将以下代码添加到config / autoload / development.local.php
return [
'view_manager' => [
'display_exceptions' => true,
],
'doctrine' => [
'connection' => [
'orm_default' => [
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => [
'host' => 'host',
'user' => 'root',
'password' => 'root',
'dbname' => 'dbname',
]
],
],
],
'db' => [
'adapters' => [
'db' => [
'driver' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'dsn' => 'mysql:dbname=dbname;host=host',
'driver_options' => [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
],
'username' => 'root',
'password' => 'root',
],
],
],
];
如何在控制器和表单中获取dbAdapter?
我想在表单过滤器中使用 NoRecordExists 验证。 像
array(
'name' => 'Zend\Validator\Db\NoRecordExists',
'options' => array(
'table' => 'test_user',
'field' => 'email',
'adapter' => $this->dbAdapter,
'exclude' => $this->excludeFields,
'messages' => array(
\Zend\Validator\Db\NoRecordExists::ERROR_RECORD_FOUND => 'Email already exists'
),
),
'break_chain_on_failure' => true
)
分享您的想法或建议在ZF3中全局配置dbAdapters。
答案 0 :(得分:1)
您需要使用工厂将适配器注入表单。我不确定是否支持使用Doctrine进行表单验证,因此我的示例将使用常规的旧zend-db。
设置一个简单的工厂,并使用依赖注入将适配器提供给表单。
表单需要接受依赖项,因此向构造函数添加一个参数:
/**
* @var \Zend\Db\Adapter\Adapter
*/
private $dbAdapter;
/**
* @param \Zend\Db\Adapter\Adapter $adapter
*/
public function __construct(\Zend\Db\Adapter\Adapter $adapter)
{
$this->dbAdapter = $adapter;
}
namespace MyModule\Factory\Form;
use Zend\ServiceManager\Factory\FactoryInterface;
/**
* Class MyFormFactory
* @package MyModule\Factory\Form
*/
class MyFormFactory implements FactoryInterface
{
/**
* Create an object
*
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
*
* @return object
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException if any other error occurs
*/
public function __invoke(
ContainerInterface $container,
$requestedName,
array $options = null
) {
/** @var \Zend\Db\Adapter\AdapterInterface $adapter */
$adapter = $container->get(AdapterInterface::class);
return new MyForm($adapter);
}
}
在你的module.config.php中,你需要告诉你的应用程序调用工厂来设置表单:
'service_manager_' => [
'factories' => [
Form\MyForm::class => Factory\Form\MyFormFactory::class
]
],
正如Dymen1提到的那样,您可以通过尝试解释此主题的tutorial in the documentation而受益。