Answer the question
In order to leave comments, you need to log in
How to add search terms to ActiveRecord in Yii2?
I have an order index page. There is a search and sorting. The controller uses the searchModel and writes data from it to the dataProvider. I need to add one more condition for selecting elements so that it is not displayed in GET. Type
$data = Orders::find()->where(['subdivision_id' => Yii::$app->user->getUserIdentity()->getSubdivisionId()])->all();
$data
again with the searchModel parameters that are stored in the GET array <?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'sender_address',
'reciever_address',
'currency_id',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
$dataProvider
store in itself $data
+ $searchModel
and in GET only data on sorting and searchModel filter would be displayed
Answer the question
In order to leave comments, you need to log in
and I fucking understand. in OrdersSearch it was necessary to dig deeper)))))
here:
public function search($params)
{
$query = Orders::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$manageer_id = 5;
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'currency_id' => $this->currency_id,
'said_weight' => $this->said_weight,
'fact_weight' => $this->fact_weight,
'pay_type' => $this->pay_type,
'dostavka_price' => $this->dostavka_price,
'pre_pay' => $this->pre_pay,
'post_pay' => $this->post_pay,
'post_pay_days' => $this->post_pay_days,
'international' => $this->international,
]);
$query->andFilterWhere(['like', 'sender_address', $this->sender_address])
->andFilterWhere(['like', 'reciever_address', $this->reciever_address])
->andFilterWhere(['like', 'sender_phone', $this->sender_phone]);
return $dataProvider;
}
}
$query = Orders::find()->where(['id' => 555, 'active' => 1]);
In serachModel, before outputting the search function, insert the condition into $query
public function search($params)
{
............
$query->andWhere(['subdivision_id' => \Yii::$app->user->getUserIdentity()->getSubdivisionId()]);
return $dataProvider;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question