4

I have two collections - Users and Order

I have a list of orders(OrderSearch model) with some users details(email, name..). Now I want to search user details relationally.

OrderSearch Model---

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

Relation in Order Model -

public function getUser(){
    return $this->hasOne(User::className(), ['_id' => 'user_id']);
}
mohit
  • 1,878
  • 1
  • 16
  • 27

0 Answers0