Answer the question
In order to leave comments, you need to log in
How to select unique values via ActiveRecord in Yii2?
It is necessary to make a selection of unique values, i.e. using DISTINCT. I have not found anywhere how this can be done using ActiveRecord without resorting to createCommand ().
Answer the question
In order to leave comments, you need to log in
// SELECT DISTINCT `user_id` ...
$query = new Query();
$query->select('user_id')->distinct();
I didn't reinvent the wheel. It turned out that displaying a list of records through a loop is so simple and easy and elementary)) Duplicate records disappear, only a list of unique records remains!
public function actionPage(){
$select = City::find()->select(['obl'])->distinct()->all();
return $this->render('page', ['select' => $select,]);
}
<select class="form-control">
<?php foreach ($select as $m) { ?>
<option value="<?=$m->obl;?>"><?=$m->obl;?></option>
<?php } ?>
</select>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question