我的 IndexController 中的专辑和类别有以下 Zend_Debug 值:
array(4) {
["id"] => string(1) "1"
["artist"] => string(18) "Paula Abdul Rashid"
["title"] => string(13) "Sunny Side Up"
["category_id"] => NULL
}
array(10) {
[0] => array(2) {
["id"] => string(1) "1"
["name"] => string(7) "Country"
}
[1] => array(2) {
["id"] => string(1) "2"
["name"] => string(7) "Hip Hop"
}
[2] => array(2) {
["id"] => string(1) "3"
["name"] => string(4) "Jazz"
}
[3] => array(2) {
["id"] => string(1) "4"
["name"] => string(14) "Latin American"
}
[4] => array(2) {
["id"] => string(1) "5"
["name"] => string(3) "Pop"
}
[5] => array(2) {
["id"] => string(1) "6"
["name"] => string(3) "R&B"
}
[6] => array(2) {
["id"] => string(1) "7"
["name"] => string(4) "Rock"
}
[7] => array(2) {
["id"] => string(1) "8"
["name"] => string(3) "Ska"
}
[8] => array(2) {
["id"] => string(1) "9"
["name"] => string(5) "Asian"
}
[9] => array(2) {
["id"] => string(2) "10"
["name"] => string(11) "Modern folk"
}
}
在我的 Form_Albums 中,我有以下表单元素:
$id = new Zend_Form_Element_Hidden('id');
$id->addFilter('Int');
$artist = new Zend_Form_Element_Text('artist');
$artist->setLabel('Artist')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$title = new Zend_Form_Element_Text('title');
$title->setLabel('Title')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$category = new Zend_Form_Element_Select('category');
$category->setLabel('Category')
->setRequired(true);
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$this->addElements(array($id, $artist, $title, $category, $submit));
我无法使用 Zend_Form_Element_Select 获得下拉列表显示。
解决方案更新:
我现在使用以下方法让它工作:
$category = new Application_Model_DbTable_Category();
$category_list = $category->findForSelect();
foreach ($category_list as $cat) { //$data is your array variable
Zend_Debug::dump($cat['id']." :: ". $cat['name']);
$form->category->addMultiOption($cat['id'], $cat['name']);
}
感谢 Rishi 提出这个想法。