How to get data from different model for select?

前端 未结 2 1452
野的像风
野的像风 2020-12-22 06:51

I have form with some attributes:

class ToraForm extends Form
{
    public function __construct($name = null)
    {
        parent::__construct(\'tora\');
           


        
相关标签:
2条回答
  • 2020-12-22 07:02

    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.

    • Zend\Form\Element\Select and Database-Values

    My BlogPost covers the following scenarios:

    • Zend\Form\Element\Select via DbAdapter
    • Zend\Form\Element\Select via TableGateway
    • DoctrineModule\Form\Element\DoctrineObject via Doctrine2

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

    Then the respective 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!
    }
    

    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;
    }
    
    0 讨论(0)
  • 2020-12-22 07:03

    My use is like that:

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

    After I use like that:

    $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()
        ),
     ));
    
    0 讨论(0)
提交回复
热议问题