コード例 #1
0
ファイル: AdminController.php プロジェクト: nagser/base
 /**
  * @param string $search
  * @param string $value
  * @param string $colAlias
  * @return Json
  * */
 public function actionSelect2List($search = '', $value = '', $colAlias = 'title')
 {
     /** @var Model $recordModel * */
     $model = $this->model;
     /** @var Model $recordModelObject * */
     $modelObject = Yii::createObject($model);
     $table = $model::tableName();
     $alias = $modelObject->isMultiLangField($colAlias) ? $colAlias : $table . '.' . $colAlias;
     $out = ['more' => false];
     if (!is_null($search)) {
         $query = new MultilingualQuery($model);
         $query->select('DISTINCT(' . $alias . ') AS id, ' . $alias . ' AS text')->from($table);
         if ($modelObject->isMultiLangField($colAlias)) {
             $query->joinWith('translation');
         }
         $query->where($alias . ' LIKE "%' . $search . '%"')->limit(20);
         $command = $query->createCommand();
         $data = $command->queryAll();
         $out['results'] = array_values($data);
     } elseif ($value > 0) {
         $out['results'] = ['id' => $value, 'text' => $model::find($value)->{$colAlias}];
     }
     return Json::encode($out);
 }