/** * Ajax поиск пользователя по его ФИО. * @param string $search * @param string $value * @param string $colAlias * @return Json * */ public function actionAjaxSearch($search = '', $value = '', $colAlias = 'title') { /** @var User $recordModel * */ $recordModel = User::className(); /** @var User $recordModelObject * */ $table = $recordModel::tableName(); $alias = $colAlias; $out = ['more' => false]; if (!is_null($search)) { $query = new MultilingualQuery($recordModel); $query->select('*, CONCAT_WS(" → ", `name`, `email`) AS text')->from($table)->joinWith('profile'); $query->where($alias . ' LIKE "%' . $search . '%"')->limit(20); $command = $query->createCommand(); $data = $command->queryAll(); // debug($data); $out['results'] = array_values($data); } elseif ($value > 0) { $out['results'] = ['id' => $value, 'text' => $recordModel::find($value)->{$colAlias}]; } return Json::encode($out); }
/** * @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); }