zf2 form:使用来自数据库的数据填充select字段

时间:2013-03-21 15:06:08

标签: php zend-framework2 zend-form zend-db-table

我正在学习zf2,而我正面临着涉及2个(最终更多)模块协同工作的问题。请注意,我仔细阅读了this post(和相关的),这对我帮助很大。我将解释一下这个问题:

  • 使用第一个模块(FrOption),管理员可以管理网站表单选项。所有选项都存储在db表中,如下所示:

ID | FIELD_NAME | FIELD_VALUE
1 |国内|德国
2 |国内|法国|
3 |性别|男|
4 |性别|女|
5 | TIPO |汽车|
6 | TIPO |飞|
...

  • 在我的模块(FrItem)中,我构建了一个需要一些" field_name"领域。 我的"项目"表格如下:

ID |名称| id_tipo |
1 |菲亚特| 5 |
2 |莎| 6 |
3 |福特| 5 |
4 |法航6 |
...

(id_tipo是选项FK)

还要考虑:

  • 我的实体有" tipo" property,setter + getter
  • 我已经建立了一个ItemHydrator来" map" id_tipo db field to" tipo"实体财产
  • 作为测试,我已在表单类中添加了此字段,并且在查看和编辑模式下一切正常:

    $this->add(
    'type' => 'Zend\Form\Element\Select',
    'name' => 'id_tipo',
    'options' => array (
        'label' => 'Tipo', 
        'empty_option' => 'Select',
        'value_options' => array ('5' => 'Car', '6' => 'Fly' ) )
    

    );

现在我想"链接"这两个模块:value_options应该是来自FrOption的动态数组,因此我正在寻找实现此要求的最佳方法。

我认为一个解决方案可能是这样的:

  1. 添加到我的FrOption / src / FrOption / Service / FrOption.php服务类getOptionByName($ fieldName)方法
  2. 在FrItem / Module.php中检索服务,然后使用getOptionByName检索数据,最后将全部注入表单。
  3. 这可能是一个明智和有效的解决方案吗?在性能方面你对它有什么看法(选项表可以增长)? 如果有的话,你用什么样的解决方案来解决类似的问题?

    由于

2 个答案:

答案 0 :(得分:9)

  

这可以通过Zend中的以下2个步骤轻松完成   Framework2第1步。

     

添加适配器的实例,实例化表单类   控制器动作

$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$form = new TestForm ($dbAdapter);
  

表单中的第2步

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;
    }

}
  

在这里,你准备好摇滚了。我希望它可以帮助你

答案 1 :(得分:-2)

<?= $form->field($client_allow_acces, '[$i]access_type')->radioList([1 => 'Allow access', 2 => 'Can\'t allow access'],[ 'item' => function($index, $label, $name, $checked, $value) {
                                    $return = '<label class="modal-radio">';
                                    $return .= '<input type="radio" name="' . $name . '" value="' . $value . '" id="custom_id_value_'.$index.'" >';
                                    $return .= '<span>  ' . ucwords($label) . '</span>';
                                    $return .= '</label>';
                                    return $return;
                             }]); ?> 
相关问题