我尝试这样做:
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;
}
我见过这种做HERE,但它不起作用。回声工作正常,但组合框没有填充。我的代码中有什么问题或什么?
谢谢! 索林
答案 0 :(得分:1)
addMultiOption($option, $value)
requires two parameters and you are giving it only a value.
你应该这样做:
foreach ($sets_table->getSets() as $set) {
$set->addMultiOption($set->id , $set->value);
}
确保你的getSets从表中返回id和值。
答案 1 :(得分: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;
}
}