如何在ZF2字段集中访问数据库适配器?

Mat*_*att 15 php zend-framework zend-form zend-db zend-framework2

我已经按照一个示例,并希望将数据库适配器传递给字段集以创建下拉菜单.

下面的代码是我如何调用fieldset.
如何访问BrandFieldset类中的数据库适配器?

$this->add(array(
    'type' => 'Application\Form\BrandFieldset',
    'name' => 'brand',
    'options' => array(
        'label' => 'Brand of the product',
    ),
));
Run Code Online (Sandbox Code Playgroud)

Mat*_*att 1

根据这些文档,我找到了解决方案。

https://framework.zend.com/manual/2.1/en/modules/zend.form.advanced-use-of-forms.html

'form_elements' => array(
    'invokables' => array(
        'fieldset' => BrandFieldsetFactory::class
    )
)
Run Code Online (Sandbox Code Playgroud)

我需要使用控制器中的服务定位器来调用表单,如下所示。

$sl = $this->getServiceLocator();
$form = $sl->get('FormElementManager')->get('Application\Form\CreateForm');
Run Code Online (Sandbox Code Playgroud)

另外,我将 __construct 更改为 init。