对于类别dropdowList我正在从数据库生成值,因为main_cat列有两次电子值,在下拉列表中它也重复两次,如何只显示一次?
+--------+-------------+---------+
| cat_id | main_cat | sub_cat |
+--------+-------------+---------+
| 1 | Electronics | Phones |
+--------+-------------+---------+
| 2 | Electronics | TVs |
+--------+-------------+---------+
| 3 | Furniture | Sofa |
+--------+-------------+---------+
我尝试了下面的代码,但它正在重复下拉列表distinct()
中的电子设备无效。
<?= $form->field($model, 'ad_cat_id',
'template' => '<label class="col-sm-3 control-label">Category</label><div class="col-sm-9">{input}{error}</div>',
])->dropDownList(
ArrayHelper::map(Categories::find()->distinct()->all(), 'cat_id', 'main_cat'),
[
prompt' => 'Please select main category',
]) ?>
如何选择main_cat列而不重复相同的值?
答案 0 :(得分:1)
用作
Categories::find()->groupBy('main_cat')->all();