How to do a search on related model in Yii 2?

$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;

And model connection ReportMailing
public function getMailing()
{
 return $this->hasOne(Mailing::className(), ['id' => 'mailing_id']);
 }

I need to find data in another table a field subject. How to do it?
March 12th 20 at 07:57
1 answer
March 12th 20 at 07:59
Solution
Change a little your code for the search model.
Something like this:
$query = ReportMailing::find()->joinWith('mailing')->where(['user_id' => Yii::$app->user->id]);

 // form $dataProvider

 $query->andFilterWhere(['like', '{{%mailing}}.subject', $this->subject]);

 return $dataProvider;


p.s. Think like there is no need
$query->andFilterWhere(['{{%mailing}}.subject' => $this->subject]);
@charlie error
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'user_id' in where clause is ambiguous - davio commented on March 12th 20 at 08:02
@davio,
->where(['{{%report_mailing}}.user_id' => Yii::$app->user->id]) // or how correctly You have a table called
- charlie commented on March 12th 20 at 08:05
@charlie
error
Operator '{{%MAILING}}.SUBJECT' requires two operands. - davio commented on March 12th 20 at 08:08
@davio, again, I think it would be more correct
$query->andFilterWhere(['{{%report_mailing}}.user_id' => Yii::$app->user->id])
- charlie commented on March 12th 20 at 08:11
@davio, remove the where clause from the query. - charlie commented on March 12th 20 at 08:14
@charlie, if you leave the search makes
$query = ReportMailing::find()->joinWith(['mailing']);
$query->andFilterWhere(['like', '{{%mailing}}.subject', $this->subject]);

if you add that does not
$query->andFilterWhere(['>=', 'created', $this->dateFrom])
 ->andFilterWhere(['<=', 'created', $this->dateBy.'23:59:59']);
- davio commented on March 12th 20 at 08:17
@charlie, works in addition to dates
$query = ReportMailing::find()->joinWith(['mailing']);
$query->andFilterWhere(['{{%report_mailing}}.user_id' => Yii::$app->user->id]);
 $query->andFilterWhere(['like', '{{%mailing}}.subject', $this->subject])
 ->andFilterWhere(['like', '{{%report_mailing}}.status', $this->status]);

 $query->andFilterWhere(['>=', '{{%report_mailing}}.created', $this->dateFrom])
 ->andFilterWhere(['<=', '{{%report_mailing}}.created', $this->dateBy.'23:59:59']);
- davio commented on March 12th 20 at 08:20
@davio, in what format You have stored date in the database and what You get it out of shape? - charlie commented on March 12th 20 at 08:23
@charlie, our database 2018-10-09 15:50:10 and the formation 2018-10-09 - davio commented on March 12th 20 at 08:26
@davio, in theory it should work
$query->andFilterWhere(['>=', '{{%report_mailing}}.created', $this->dateFrom . '00:00:00'])
 ->andFilterWhere(['<=', '{{%report_mailing}}.created', $this->dateBy . '23:59:59']);

The space before the time set, and then in Your case it turns out "2018-10-0915:50:10" - charlie commented on March 12th 20 at 08:29
@davio, about working with associated models is there a good webinar. - charlie commented on March 12th 20 at 08:32
@charlie, I solved the problem, I had a bad validation so I changed it
[['dateBy', 'dateFrom'], 'date', 'format' => 'php:Y-m-d'],
and still does. Thanks for the help! - davio commented on March 12th 20 at 08:35
@daviothat shows so
echo $this->dateBy . '23:59:59';
and so
echo $this->dateBy . '23:59:59'
?
What is the data type in the table to date? - charlie commented on March 12th 20 at 08:38

Find more questions by tags Yii