如何访问 ZF2 Field Set 中的数据库适配器?
How can I access Database Adapter in ZF2 Field Set?
我按照一个示例进行操作,想将数据库适配器传递给字段集以创建下拉菜单。
下面的代码是我调用字段集的方式。
我如何访问 BrandFieldset class 中的数据库适配器?
$this->add(array(
'type' => 'Application\Form\BrandFieldset',
'name' => 'brand',
'options' => array(
'label' => 'Brand of the product',
),
));
实例化字段集是框架 FormElementManager. When you try to access a form, form element or fieldset, the FormElementManager
knows where to find and how to create it. This behaviour summerized in Default Services 部分的职责。
由于访问表单元素的正确方法是从 FormElementManager 中检索它们,因此我会编写一个 BrandFieldsetFactory
来注入该 DB 适配器或进一步依赖于构造字段集以实现此目的。
ZF3 友好的字段集工厂看起来像:
<?php
namespace Application\Form\Factory;
use Application\Form\BrandFieldset;
use Interop\Container\ContainerInterface;
class BrandFieldsetFactory
{
/**
* @return BrandFieldset
*/
public function __invoke(ContainerInterface $fem, $name, array $options = null)
{
// FormElementManager is child of AbstractPluginManager
// which makes it a ContainerInterface instance
$adapter = $fem->getServiceLocator()->get('Your\Db\Adapter');
return new BrandFieldset($adapter);
}
}
此时,BrandFieldset
应该扩展 Zend\Form\Fieldset\Fieldset
,它的构造函数可能如下所示:
private $dbAdapter;
/**
* {@inheritdoc}
*/
public function __construct(My/Db/Adapter $db, $options = [])
{
$this->dbAdapter = $db;
return parent::__construct('brand-fieldset', $options);
}
最后,在 module.config.php
文件中,我有一个配置告诉 FormElementManager
关于这个工厂:
<?php
use Application\Form\BrandFieldset;
use Application\Form\Factory\BrandFieldsetFactory;
return [
// other config
// Configuration for form element manager
'form_elements' => [
'factories' => [
BrandFieldset::class => BrandFieldsetFactory::class
],
],
];
HINT: The BrandFieldset::init()
method will be called automatically by FormElementManager after construction. You can put any post-initialization logic into this method.
根据这些文档,我找到了解决方案。
https://framework.zend.com/manual/2.1/en/modules/zend.form.advanced-use-of-forms.html
'form_elements' => array(
'invokables' => array(
'fieldset' => BrandFieldsetFactory::class
)
)
我需要使用控制器中的服务定位器调用表单,如下所示。
$sl = $this->getServiceLocator();
$form = $sl->get('FormElementManager')->get('Application\Form\CreateForm');
此外,我将 __construct 更改为 init。
我按照一个示例进行操作,想将数据库适配器传递给字段集以创建下拉菜单。
下面的代码是我调用字段集的方式。
我如何访问 BrandFieldset class 中的数据库适配器?
$this->add(array(
'type' => 'Application\Form\BrandFieldset',
'name' => 'brand',
'options' => array(
'label' => 'Brand of the product',
),
));
实例化字段集是框架 FormElementManager. When you try to access a form, form element or fieldset, the FormElementManager
knows where to find and how to create it. This behaviour summerized in Default Services 部分的职责。
由于访问表单元素的正确方法是从 FormElementManager 中检索它们,因此我会编写一个 BrandFieldsetFactory
来注入该 DB 适配器或进一步依赖于构造字段集以实现此目的。
ZF3 友好的字段集工厂看起来像:
<?php
namespace Application\Form\Factory;
use Application\Form\BrandFieldset;
use Interop\Container\ContainerInterface;
class BrandFieldsetFactory
{
/**
* @return BrandFieldset
*/
public function __invoke(ContainerInterface $fem, $name, array $options = null)
{
// FormElementManager is child of AbstractPluginManager
// which makes it a ContainerInterface instance
$adapter = $fem->getServiceLocator()->get('Your\Db\Adapter');
return new BrandFieldset($adapter);
}
}
此时,BrandFieldset
应该扩展 Zend\Form\Fieldset\Fieldset
,它的构造函数可能如下所示:
private $dbAdapter;
/**
* {@inheritdoc}
*/
public function __construct(My/Db/Adapter $db, $options = [])
{
$this->dbAdapter = $db;
return parent::__construct('brand-fieldset', $options);
}
最后,在 module.config.php
文件中,我有一个配置告诉 FormElementManager
关于这个工厂:
<?php
use Application\Form\BrandFieldset;
use Application\Form\Factory\BrandFieldsetFactory;
return [
// other config
// Configuration for form element manager
'form_elements' => [
'factories' => [
BrandFieldset::class => BrandFieldsetFactory::class
],
],
];
HINT: The
BrandFieldset::init()
method will be called automatically by FormElementManager after construction. You can put any post-initialization logic into this method.
根据这些文档,我找到了解决方案。
https://framework.zend.com/manual/2.1/en/modules/zend.form.advanced-use-of-forms.html
'form_elements' => array(
'invokables' => array(
'fieldset' => BrandFieldsetFactory::class
)
)
我需要使用控制器中的服务定位器调用表单,如下所示。
$sl = $this->getServiceLocator();
$form = $sl->get('FormElementManager')->get('Application\Form\CreateForm');
此外,我将 __construct 更改为 init。