如何从zf2中的数据库加载选择选项 [英] How to load select option from database in zf2
问题描述
我一直在关注 博客的 zf2 指南 我已经创建了所有控制器、工厂、表单、映射器、模型、服务、视图等
I Have been following zf2 guide for blog I have created everything Controller, Factory, Form, Mapper, Model, Service, view etc
在我的表单中,我有一个选择元素
In my form I have a select element
$this->add(array(
'type' => 'select',
'name' => 'roleId',
'attributes' => array(
'id' => 'roleId',
'options' => array(
'1' => 'Admin',
'2' => 'Manager',
),
),
'options' => array(
'label' => 'Role',
),
));
现在以这种形式,我想从数据库中加载角色的选项.
我尝试通过创建一个简单的函数来加载选项,该函数可以在元素中访问,如下所示,但我无法获取结果.我已经创建了 Controller、Factory、Form、Mapper、Model、Service 和 view,我可以在其中对 Role 进行 CRUD 操作.
Now in this form I want to load the option for the role from the database.
I tried loading the option by creating a simple function, which can be accessed in the element as below, but Am not able to fetch the result. I have already created Controller, Factory, Form, Mapper, Model, Service and view, Where I can do CRUD operation on Role.
$this->add(array(
'type' => 'select',
'name' => 'roleId',
'attributes' => array(
'id' => 'roleId',
'options' => $this->getAllRoles(),
),
'options' => array(
'label' => 'Role',
),
));
public function getAllRoles()
{
$roles = $this->getServiceLocator()->get('Admin\Service\RoleService');
$allRoles = $this->getAllTheRoles();
return $allroles;
}
任何人都可以指导我如何加载索引操作中列出的所有角色中的选项 博客帖子 带有 ID 和角色名称.
Can anybody guide me how can I load all the Roles in option as listed in the IndexAction following Blog Post with ID and Name of the Role.
推荐答案
您可以创建一个预先填充了角色的可重用表单元素.为此,您必须使用 module.config.php
中的表单元素管理器注册该服务.
You could create a reusable form element that is pre-populated with the roles. To do so you must register the service with the form element manager in module.config.php
.
return [
'form_elements' => [
'factories' => [
'RoleSelect' => 'MyModule\Form\Element\RoleSelectFactory',
],
],
];
不需要扩展标准选择类,因为更改只是配置.这最好在工厂类中完成.
There is not need to extend the standard select class as the changes are configuration only. This is best done in a factory class.
namespace MyModule\Form\Element;
use Zend\Form\Element\Select;
class RoleSelectFactory
{
public function __invoke($formElementManager, $name, $rname)
{
$select = new Select('role_id');
$select->setOptions(['label' => 'Role']);
$select->setAttributes(['id' => 'role_id']);
$serviceManager = $formElementManager->getServiceLocator();
$roleService = $serviceManager->get('Admin\Service\RoleService');
$options = [];
foreach($roleService->getAllTheRoles() as $role){
$options[$role->getId()] => $role->getName();
}
$select->setValueOptions($options);
return $select;
}
}
然后可以更新在表单中添加元素以使用我们注册的服务的名称.
Adding the element within a form can then be updated to use the name of the service we registered.
$this->add([
'name' => 'role_id'
'type' => 'RoleSelect',
]);
要记住的重要一点是,必须使用 $formElementManager->get('FormWithRoleSelect')
创建使用此元素的 表单.
One important point to remember is that the form using this element must be created using the $formElementManager->get('FormWithRoleSelect')
.
这篇关于如何从zf2中的数据库加载选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!