Answer the question
In order to leave comments, you need to log in
How to filter by link click Yii2?
Another question about data filtering. Here is the screenshot:
I want to flirt only the user's patients by pressing the "my patients" button.
i.e. when the button is pressed, it should first be filtered by the user, and then by the Last name and first name
<?php
namespace frontend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use frontend\models\History;
/**
* SearchPacients represents the model behind the search form of `frontend\models\History`.
*/
class SearchPacients extends History
{
public $family;
public $name;
public $otchestvo;
public $filtr;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'pacient_id', 'standart_id', 'doctor_id', 'otdelenie_id', 'status_id', 'type_analiz_id', 'organization_id', 'napravlenie_id', 'lu'], 'integer'],
[['date','family' ,'name','date_close'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = History::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes' => [
'id',
'family' => [
'label' => 'Фамилия',
'default' => SORT_ASC
],
'name' => [
'label' => 'Имя',
'default' => SORT_ASC
],
'otchestvo' => [
'label' => 'Отчество',
'default' => SORT_ASC
],
'dr' => [
'label' => 'Дата рождение',
'default' => SORT_ASC
],
]
]);
if (!($this->load($params) && $this->validate())) {
/**
* Жадная загрузка данных модели Страны
* для работы сортировки.
*/
$query->joinWith(['pacients']);
return $dataProvider;
}
//$this->addCondition($query, 'id');
// $this->addCondition($query, 'family', true);
$this->filtr=Yii::$app->user->id;
$query->andFilterWhere(['doctor_id'=>$this->filtr]);
// Фильтр по стране
$query->joinWith(['pacients' => function ($q) {
$q->where('pacients.family LIKE "%' . $this->family. '%"');
}]);
$query->joinWith(['pacients' => function ($q) {
$q->where('pacients.name LIKE "%' . $this->name. '%"');
}]);
$query->joinWith(['pacients' => function ($q) {
$q->where('pacients.otchestvo LIKE "%' . $this->otchestvo. '%"');
}]);
return $dataProvider;
}
}
Answer the question
In order to leave comments, you need to log in
The Search model builds filtering based on the data that came in the get request, so just form the desired link, which will contain the necessary parameters.
Something like
Html::a('моиПациенты',['someController/someAction','MySearchModelName'=>[
'doctor_id' => Yii::$app->user->id
]])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question