Skip to content

Instantly share code, notes, and snippets.

@igorvolnyi
Last active March 13, 2018 09:02
Show Gist options
  • Save igorvolnyi/ffb7265c777d6254a4296111109162e0 to your computer and use it in GitHub Desktop.
Save igorvolnyi/ffb7265c777d6254a4296111109162e0 to your computer and use it in GitHub Desktop.
AJAX find persons
/**
* Returns persons via AJAX.
* @return mixed JSON data
*/
public function actionFindAjax($q = null) {
if(Yii::$app->request->isAjax && !empty($q)) {
$out = ['results' => ['id' => '', 'text' => '']];
Yii::$app->response->format = Response::FORMAT_JSON;
$query = new Query();
$result = $query->select([
'id' => 'person.id',
'text' => "CONCAT(person.fio, ' (', department.name, ', ', job.title, ')' )"
])
->from('person')
->leftJoin('job', 'person.job_id = job.id')
->leftJoin('department', 'job.department_id = department.id')
->orderBy('person.fio')
->where(['like', 'person.fio', $q])
->all();
$out['results'] = array_values($result);
return $out;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment