我正在为网站编制折扣,我遇到了multiSelect框选项的问题。我试图通过硬编码获得价值,例如:
$elements['apply_to'] = new \Zend_Form_Element_Multiselect('apply_to');
$elements['apply_to']->setMultiOptions(
array(
'iframe' => $translate->_('apply_to_iframe'),
'iframer' => $translate->_('apply_to_iframer'),
'unregistered' => $translate->_('apply_to_unregistered'),
'person' => $translate->_('apply_to_person'),
'agency' => $translate->_('apply_to_agency')
)
);
$elements['apply_to']->setLabel('apply_to')
->setView($view)
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator(new \Zend_Validate_NotEmpty(\Zend_Validate_NotEmpty::PHP))
->setAttribs(array(
'translate' => $translate
))
->setDecorators(
array(
array('ViewScript',
array(
'viewScript' => 'Components/Forms/Elements/Multiselect.phtml'
)
)
)
);
从db模型中获取值:
$elements['apply_to'] = new \Desk\Components\Forms\Elements\Multiselect('apply_to');
$elements['apply_to']->setMultiOptions(\Desk\Models\Discounts::getApplyTo($translate));
$elements['apply_to']->setLabel('apply_to')
->setView($view)
->setAttribs(array(
'translate' => $translate
))
->setDecorators(
array(
array('ViewScript',
array(
'viewScript' => 'Components/Forms/Elements/Multiselect.phtml'
)
)
)
);
其中getApplyTo是:
public static function getApplyTo($translate, $not_deleted = false){
$db = new \Db\Users();
if ($not_deleted) {
$rows = $db->fetchAll(array('deleted IS NULL'));
} else {
$rows = $db->fetchAll();
}
$return = array();
if (count($rows) > 0) {
$rows = $rows->toArray();
foreach ($rows as $row) {
$return[ $row['user_type'] ] = $row['user_type'];
}
}
return $return;
}
但是没有一个例子可行。任何人都可以帮助我吗?