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

Zend framework2 如何从不同的模型中获取数据以供选择?,zend-framework2,zend-form2,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',

我有一些属性的表单:

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',
            ),
        ));
}
但我想添加下拉列表,其中包含从另一个模型获取的数据。怎么做?

我的用法是:

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()
    ),
 ));

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

我的博客文章涵盖了以下场景:

  • Zend\Form\Element\Select通过DbAdapter
  • Zend\Form\Element\Select via TableGateway
  • 通过Doctrine2的DoctrineModule\Form\Element\DoctrineObject
在这里,我将只演示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;
}

我想知道我应该把他的
getoptions for select
函数放在哪里,如何调用
add.phtml和edit.phtml