如何从不同的模型中获取数据以供选择?

时间:2013-04-15 18:31:34

标签: zend-framework2 zend-form2

我的表格有一些属性:

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',
            ),
        ));
}

但我希望添加下拉列表,其中包含从其他模型中获取的数据。怎么做?

2 个答案:

答案 0 :(得分:1)

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

我的BlogPost涵盖以下场景:

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

这里我将仅演示DbAdapter方法,但没有太多解释。请参阅我的博客文章以获得深入的解释。

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函数:

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

答案 1 :(得分:0)

我的用法是这样的:

Class Useful{
/**
 * All languages
 * @return array 
 */
public static function getLanguages(){
  return array(
    'fr_DZ'=>'Algeria - Français',
    'es_AR'=>'Argentina - Español',
    'en_AU'=>'Australia - English',
    'nl_BE'=>'België - Nederlands',
    'fr_BE'=>'Belgique - Français',
    'es_BO'=>'Bolivia - Español',
    'bs_BA'=>'Bosna i Hercegovina - Hrvatski',
  ...
  );
 }
}

我这样使用之后:

$this->add(array(
  'type' => 'Zend\Form\Element\Select',
  'name' => 'languages',
  'attributes'=>array(
     'multiple'=>"multiple",
   ),
   'options'       => array(
     'label'             => 'My languages I speak',
     'description'       => '',
     'value_options'     => Useful::getLanguages()
    ),
 ));