zf2 表单:使用来自数据库的数据填充选择字段 [英] zf2 form: populate select field with data coming from database
问题描述
我正在学习 zf2,但遇到了一个涉及 2 个(最终更多)模块协同工作的问题.请注意,我已经仔细阅读了 this帖子(以及相关的帖子)对我帮助很大.我要解释一下这个问题:
- 使用第一个模块 (FrOption),管理员可以管理网站表单选项.所有选项都存储在一个数据库表中,如下所示:
id|field_name|field_value
1|国家|德国|
2|国家|法国|
3|性别|男|
4|性别|女|
5|tipo|汽车|
6|tipo|飞|
...
- 在我的模块 (FrItem) 中,我构建了一个需要一些field_name"字段的表单.我的项目"表如下:
id|name|id_tipo|
1|菲亚特|5|
2|汉莎航空|6|
3|福特|5|
4|法航6|
...
(id_tipo 是一个选项 FK)
还要考虑:
- 我的实体具有tipo"属性,setter + getter
- 我已经构建了一个 ItemHydrator 来将 id_tipo db 字段映射"到tipo"实体属性
作为测试,我在表单类中添加了此字段,并且在查看和编辑模式下一切正常:
$this->add('类型' =>'Zend\Form\Element\Select','名称' =>'id_tipo','选项' =>大批 ('标签' =>'蒂波','empty_option' =>'选择','value_options' =>数组 ('5' => '汽车', '6' => '飞') )
);
现在我想链接"这两个模块:value_options 应该是来自 FrOption 的动态数组,所以我正在寻找实现此要求的最佳方法.
我认为一种解决方案可能是这样的:
- 将 getOptionByName($fieldName) 方法添加到我的 FrOption/src/FrOption/Service/FrOption.php 服务类
- 在 FrItem/Module.php 中检索服务,然后使用 getOptionByName 获取数据,最后将所有内容注入表单.
这会是一个明智且有效的解决方案吗?就性能而言,您对此有何看法(选项表可能会增长)?如果有,你用过什么样的解决方案来解决类似的问题?
谢谢
这可以通过在 Zend 中按照以下 2 个步骤轻松完成Framework2 第 1 步.
add 适配器的实例是什么实例化表单类中的控制器动作
$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');$form = new TestForm ($dbAdapter);
<块引用>
表单中的第 2 步
namespace Test\Form;使用 Zend\Form\Form;使用 Zend\Db\Adapter\AdapterInterface;使用 Zend\Db\Adapter\Adapter;类 TestForm 扩展表单{受保护的 $ 适配器;公共函数 __construct(AdapterInterface $dbAdapter){$this->adapter =$dbAdapter;parent::__construct("测试表格");$this->setAttribute('method', 'post');//您的选择字段$this->add(array('类型' =>'Zend\Form\Element\Select','名称' =>'姓名','tabindex' => 2,'选项' =>大批('标签' =>'作者','empty_option' =>'请选择作者','value_options' =>$this->getOptionsForSelect(),)));//其他字段}公共函数 getOptionsForSelect(){$dbAdapter = $this->adapter;$sql = 'SELECT id,name FROM newsauthor where active=1 ORDER BY sortorder ASC';$statement = $dbAdapter->query($sql);$result = $statement->execute();$selectData = array();foreach ($result as $res) {$selectData[$res['id']] = $res['name'];}返回 $selectData;}}
<块引用>
好了,你已经准备好摇滚了.我希望它对你有帮助
I'm learning zf2 and I'm facing a problem involving 2 (eventually more) modules working together. Note, I've carefully read this post (and the related one) which helped me a lot. I'm going to explain a bit the problem:
- Using the first module (FrOption) an administrator can manage website form options. All options are stored in a db table like this:
id|field_name|field_value
1|country|germany|
2|country|france|
3|gender|Male|
4|gender|Female|
5|tipo|Car|
6|tipo|Fly|
...
- In my module (FrItem) I've built a form which needs some "field_name" fields. My "item" table is the following:
id|name|id_tipo|
1|Fiat|5|
2|Lufthansa|6|
3|Ford|5|
4|Air France 6|
...
(id_tipo is an option FK)
Also consider:
- My entity has the "tipo" property, setter + getter
- I've built an ItemHydrator to "map" id_tipo db field to "tipo" entity property
As a test, I've added this field in my form class and everything works fine both in view and edit mode:
$this->add( 'type' => 'Zend\Form\Element\Select', 'name' => 'id_tipo', 'options' => array ( 'label' => 'Tipo', 'empty_option' => 'Select', 'value_options' => array ('5' => 'Car', '6' => 'Fly' ) )
);
Now i want to "link" the two modules: value_options should be a dynamic array coming from FrOption so I'm looking for the the best way to accomplish this requirement.
I thought one solution could be something like this:
- Add to my FrOption/src/FrOption/Service/FrOption.php Service class the getOptionByName($fieldName) method
- In FrItem/Module.php retrieve the Service, then the data using getOptionByName and finally inject all into the Form.
Could this be a wise and working solution? What do you thing about it also in terms of performance (option table could grow)? If any, what kind of solution have you used to solve a similar problem?
Thanks
This can be done easily by following the below 2 steps in Zend Framework2 Step 1.
add The instance of the adapter what instantiating form class in the controller action
$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$form = new TestForm ($dbAdapter);
Step 2 in your form
namespace Test\Form;
use Zend\Form\Form;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\Adapter\Adapter;
class TestForm extends Form
{
protected $adapter;
public function __construct(AdapterInterface $dbAdapter)
{
$this->adapter =$dbAdapter;
parent::__construct("Test Form");
$this->setAttribute('method', 'post');
//your select field
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'name',
'tabindex' =>2,
'options' => array(
'label' => 'Author',
'empty_option' => 'Please select an author',
'value_options' => $this->getOptionsForSelect(),
)
));
// another fields
}
public function getOptionsForSelect()
{
$dbAdapter = $this->adapter;
$sql = 'SELECT id,name FROM newsauthor where active=1 ORDER BY sortorder ASC';
$statement = $dbAdapter->query($sql);
$result = $statement->execute();
$selectData = array();
foreach ($result as $res) {
$selectData[$res['id']] = $res['name'];
}
return $selectData;
}
}
And here you go you are ready to rock.I hope it helps you
这篇关于zf2 表单:使用来自数据库的数据填充选择字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!