Answer the question
In order to leave comments, you need to log in
How to search by related model in Yii2?
$query = ReportMailing::find()->where(['user_id' => Yii::$app->user->id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => Yii::$app->params['showEntriesDefault'],
'forcePageParam' => false,
'pageSizeParam' => false,
],
'sort' => [
'defaultOrder' => [
'created' => SORT_DESC,
]
],
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere(['like', 'mailing_id', $this->subject]);
return $dataProvider;
public function getMailing()
{
return $this->hasOne(Mailing::className(), ['id' => 'mailing_id']);
}
Answer the question
In order to leave comments, you need to log in
Change your search model code a bit.
More or less like this:
$query = ReportMailing::find()->joinWith('mailing')->where(['user_id' => Yii::$app->user->id]);
// формируете $dataProvider
$query->andFilterWhere(['like', '{{%mailing}}.subject', $this->subject]);
return $dataProvider;
$query->andFilterWhere(['{{%mailing}}.subject' => $this->subject]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question