为每个ZEND添加元素


addElement foreach ZEND

早上好,尝试为数据库创建一个动态选择值,但是您可以添加foreach这个选择?

$table = new Application_Model_DbTable_Artista();
$nombre = array();
        foreach ($table->listar() as $c)
        {    
            $id = $c->id;
            $nombre[$id] = $c->nombre;
        }

$this->addElement('select','artista_id',
                          array(
                                 'value' => 'white',
                                 'multiOptions' => array($nombre
                                                         ),
                                 'decorators'   =>  array('ViewHelper', 'Errors'),
                                 )
                         );

完全可以。

        $roles = $roleTable->fetchAll();
        $selRol = array();
        foreach($roles as $role) {
            $id = $role->getId();
            $selRol[$id] = $role->getName();
        }
        $this->setAttribute('method', 'post');
        $this->add(array(
            'name' => 'username',
            'attributes' => array(
                'type'  => 'text',
            ),
            'options' => array(
                'label' => 'Usuario',
            ),
        ));
        $this->add(array(
            'name' => 'password',
            'attributes' => array(
                'type'  => 'password',
            ),
            'options' => array(
                'label' => 'Clave',
            ),
        ));
        $this->add(array(
            'name' => 'email',
            'attributes' => array(
                'type'  => 'email',
            ),
            'options' => array(
                'label' => 'Email',
            ),
        ));
        $this->add(array(
            'type' => 'Zend'Form'Element'Select',
            'name' => 'role_id',
            'options' => array(
                'label' => 'Role',
                'value_options' => $selRol,
             ),
        ));

来源:https://github.com/FraGoTe/admin-dashboard-zend/blob/master/module/Dashboard/src/Dashboard/Form/UserForm.php