-1

我尝试这样做:

public function init()
{
    /* Form Elements & Other Definitions Here ... */

    $sets_table = new Optionals_Model_DbTable_Sets();

    $set = new Zend_Form_Element_Select('set');
    $set ->setLabel('Alegeti setul de optionale:');

    foreach ($sets_table->getSets() as $value) {
        echo $value->cod_set_optional;
        $set->addMultiOption($value->cod_set_optional);
    }

    $submit = new Zend_Form_Element_Submit('Continua');

    $this->addElements ( array (
            $set,
            $submit
    ) );
}

DbTable 中的 getSets() 如下所示:

public function getSets()
{
    $select = $this->select();
    $rows = $this->fetchAll($select);
    if (!$rows) {
        throw new Exception("Could not find!");
    }
    return $rows;
}

我在这里看到过这种做法,但它不起作用。回声工作正常,但未填充组合框。我的代码有问题还是什么?

谢谢!索林

4

2 回答 2

1

addMultiOption($option, $value) 需要两个参数,而您只给它一个值。

你应该做这个 :

 foreach ($sets_table->getSets() as $set) {
        $set->addMultiOption($set->id , $set->value);
    }

确保您的 getSets 从您的表中返回一个 id 和一个值。

于 2012-04-13T12:01:43.217 回答
0

如果您使用 fetchAll 方法只是使用

$list->addMultiOptions(usermodel::getInstance()->getusers());

整个代码是这样的

$list      =   new Zend_Form_Element_Select('users');
$list->setLabel('Select a user')
          ->setRequired(true)
          ->addValidators(array(array('notEmpty',true, array('messages' => array('isEmpty' => 'Please select a User')))));
$list->addMultiOptions(usermodel::getInstance()->getusers());
die($list);

并且在模型中

class usermodel extends Zend_Db_Table
{
    protected  $_name               =   'users';
    protected  static $_instance    =   null;

    public function getusers()
    {
        $sql          =     "SELECT us.`id` AS `key`,CONCAT(us.`firstname`,' ',us.`lastname`) AS `value` FROM   
     `users` us 
                            ORDER BY us.`firstname` ASC";  
        return  $this->getAdapter()->fetchAll($sql);
    }

    public static function getInstance(){
         if( !isset(self::$_instance) ){
            $instance = new self();
            self::$_instance = $instance;
         }
         return self::$_instance;
    }

}
于 2013-02-19T07:21:56.120 回答