Zend framework 1.9从数据库查询填充复选框


Zend Framerwork 1.9 populate checkbox from database query

使用Zend framework 1.9。我有一个表在db与许多列。我只获取某些列:

$select = $table->select();
$select->from($table, array('skill_id', 'description'))->where('parent_skill IS NULL');
$rows = $table->fetchAll($select);
print_r($rows->toArray());
//This is the output :
Array ( [0] => Array ( [skill_id] => 1 [description] => Soccorso stradale ) [1] => Array ( [skill_id] => 4 [description] => Carrozziere ))

现在我想填充一个复选框元素:

$form->skills->setMultiOptions ( $rows->toArray () );

我的问题是方法setMultiOptions期望数组array(key=>value)。是否可以将我的$rows数组转换为格式良好的数组?

尝试像这样为选项构造数组:

$row_options = array();
foreach($rows->toArray() as $key => $val){
    $row_options[$val['skill_id']] =  $val['description'];
}
$form->skills->setMultiOptions ($row_options);

嗯,是的,我猜

    foreach($rows->toArray() as $subArray){
        foreach($subArray as $val){
            $newArray[] = $val;
        }
    } 
print_r($newArray);die;

您可以使用 fetchPair :

……

$select->from($table, array('skill_id', 'description'))->where('parent_skill IS NULL');

$ = $表行-> fetchPair (选择美元);

形式->技能——> setMultiOptions ($ row_options);