如何从不同的模型中获取数据以进行选择? [英] How to get data from different model for select?

查看:128
本文介绍了如何从不同的模型中获取数据以进行选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格具有一些属性:

I have form with some attributes:

class ToraForm extends Form
{
    public function __construct($name = null)
    {
        parent::__construct('tora');
        $this->setAttribute('method', 'post');

        $this->add(array(
            'name' => 'id',
            'attributes' => array(
                'type' => 'hidden',
            ),
        ));
        $this->add(array(
            'name' => 'name',
            'attributes' => array(
                'type' => 'text',
                'required' => true,
            ),
            'options' => array(
                'label' => 'name',
            ),
        ));
}

但是我想添加包含来自另一模型的数据的下拉列表.怎么做?

but I want add drop-down list with data taken from another model. How to do it?

推荐答案

您可以采用几种不同的方法.最终,您的表单具有依赖项,需要将其注入.我写了一篇深入的博客文章,介绍选择列表的三个最常见的表单依赖用例.

There are several different approaches you can take. Ultimately your Form has a dependency, which needs to be injected. I have written an in-depth blog-post about the three most common use-cases for Form-Dependencies for a Select-List.

我的BlogPost涵盖以下情况:

My BlogPost covers the following scenarios:

  • Zend \ Form \ Element \通过DbAdapter选择
  • Zend \ Form \ Element \通过TableGateway选择
  • 通过Doctrine2的DoctrineModule \ Form \ Element \ DoctrineObject

在这里,我将仅演示DbAdapter方法,而无需过多解释.请参阅我的博客文章以获取详细说明.

Here i will demonstrate only the DbAdapter approach without much explanation. Please refer to my blogpost for the in-depth explanations.

public function formDbAdapterAction()
{
    $vm = new ViewModel();
    $vm->setTemplate('form-dependencies/form/form-db-adapter.phtml');

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

        return $vm->setVariables(array(
        'form' => $form
    ));
}

然后是相应的表单类:

class DbAdapterForm extends Form
{
    protected $dbAdapter;

    public function __construct(AdapterInterface $dbAdapter)
    {
        $this->setDbAdapter($dbAdapter);

        parent::__construct('db-adapter-form');

        $this->add(array(
            'name'    => 'db-select',
            'type'    => 'Zend\Form\Element\Select',
            'options' => array(
                'label'         => 'Dynamic DbAdapter Select',
                'value_options' => $this->getOptionsForSelect(),
                'empty_option'  => '--- please choose ---'
            )
        ));
    }

    // more later...

    // Also: create SETTER and GETTER for $dbAdapter!
}

最后但并非最不重要的DataProvider函数:

And last but not least the DataProvider function:

public function getOptionsForSelect()
{
    $dbAdapter = $this->getDbAdapter();
    $sql       = 'SELECT t0.id, t0.title FROM selectoptions t0 ORDER BY t0.title ASC';
    $statement = $dbAdapter->query($sql);
    $result    = $statement->execute();

    $selectData = array();

    foreach ($result as $res) {
        $selectData[$res['id']] = $res['title'];
    }

    return $selectData;
}

这篇关于如何从不同的模型中获取数据以进行选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆