下拉列表唯一值yii2

时间:2018-06-28 02:47:37

标签: postgresql yii2 dropdown

我尝试从我的postgres数据库中创建一个下拉列表,而数据库就是这样

| id_provinsi | id_waktu |  id_kbli |
|     1000    |   12007  |     A    |
|     1000    |   12007  |     A1   |
|     1000    |   12007  |     A2   |
|     1000    |   12008  |     A    |
|     1000    |   12008  |     A1   |
|     1000    |   12008  |     A2   |
|     1100    |   12007  |     A    |
|     1100    |   12007  |     A1   |
|     1100    |   12007  |     A2   |
|     1100    |   12008  |     A    |
|     1100    |   12008  |     A1   |
|     1100    |   12008  |     A2   |

,我想获得一个显示id_waktu的下拉列表

| 12007 |
| 12008 |

我的代码:

$pdrb=PdrbProv::find()->groupBy('id_waktu')->all();
$listData=ArrayHelper::map($pdrb,'id_provinsi','id_waktu');

echo $form->field($model, 'id_waktu')->dropDownList(
    $listData,
    ['prompt'=>'Select...']
    );

但出现以下错误:

QLSTATE[42803]: Grouping error: 7 ERROR: column "pdrb_prov.id_provinsi" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT * FROM "pdrb_prov" GROUP BY "id_waktu"
^
The SQL being executed was: SELECT * FROM "pdrb_prov" GROUP BY "id_waktu"

我该怎么办?有人可以帮忙吗?谢谢

2 个答案:

答案 0 :(得分:0)

$pdrb = PdrbProv::find()->select(['id_provinsi','id_waktu'])->distinct()->all();

答案 1 :(得分:0)

尝试这些查询

$pdrb = PdrbProv::find()
           ->select('id_provinsi, id_waktu')
           ->groupBy('id_waktu')
           ->all();