【问题标题】:How to get data from different model for select?如何从不同的模型中获取数据以供选择?
【发布时间】:2013-04-15 18:31:34
【问题描述】:

我有一些属性的表单:

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

但我想添加下拉列表,其中包含取自另一个模型的数据。怎么办?

【问题讨论】:

    标签: zend-framework2 zend-form2


    【解决方案1】:

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

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

    • Zend\Form\Element\通过 DbAdapter 选择
    • Zend\Form\Element\通过 TableGateway 选择
    • DoctrineModule\Form\Element\DoctrineObject 通过 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
        ));
    }
    

    然后是各自的Form Class:

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

    【讨论】:

    • 我想知道我应该把他的getOptionsForSelect函数放在哪里,我怎样才能调用add.phtml and edit.phtml
    【解决方案2】:

    我的使用是这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      相关资源
      最近更新 更多