我有两个集合 - 用户和订单
我有一个订单列表(OrderSearch 模型),其中包含一些用户详细信息(电子邮件、姓名..)。现在我想相关地搜索用户详细信息。
订单搜索模型---
public function search($params)
{
// Email getting from query params
if(!empty($params['email'])){
$this->email = $params['email'];
}
$query = Order::find();
$query->with(['user']); // for related document
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pagesize' => 10,
],
]);
$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;
}
$query->andFilterWhere([
'id' => isset($this->id)?(int)$this->id:$this->id,
'status' => $this->status,
'user_id' => $this->user_id,
]);
$query->andFilterWhere(['like', 'sku', $this->sku])
->andFilterWhere(['like', 'message', $this->message])
// Tried below code but it is not working.
->andFilterWhere(['like', 'user.email', $this->email]); // $this->email is email which I want to search
return $dataProvider;
}
订单模型中的关系 -
public function getUser(){
return $this->hasOne(User::className(), ['_id' => 'user_id']);
}