$query = DishesGroupSm::find()->select('group_id')->distinct();
[
'attribute' => 'groups.title'
],
>
$query = DishesGroupSm::find()->distinct(['group_id']);
public function actionCreate()
{
$model = new DishesGroupSm();
$dishesItems = ArrayHelper::map(Dishes::find()->all(), 'id','title');
$groupsItems = ArrayHelper::map(Groups::find()->all(), 'id','title');
if ($model->load(Yii::$app->request->post())) {
$dishes = Yii::$app->request->post('DishesGroupSm')['dishes_id'];
$group = Yii::$app->request->post('DishesGroupSm')['group_id'];
DishesGroupSm::deleteAll(['group_id' => $group]);
foreach($dishes as $item){
$groupItem = new DishesGroupSm();
$groupItem->dishes_id = $item;
$groupItem->group_id = $group;
$groupItem->save();
}
return $this->redirect('index');
} else {
return $this->render('create', [
'model' => $model,
'dishesItems' => $dishesItems,
'groupsItems' => $groupsItems
]);
}
}