Mongodb Relation Problem

Dear All:

I’m using mongodb in my project.

I have two AR models.

Displaying in one gridview seems work.

But when i try to search fields about relational collection, it’s not working.

Below is my code:




    public $order_status;


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['type', 'floor', 'order_status'], 'safe'],

        ];

    }


    public function getOrder()

    {

        return $this->hasOne(Order::className(), ['main_item' => '_id']);

    }


    public function search($params, $pid)

    {

        $query = static::find()->with('order')->filterWhere(['pid' => $pid, 'type' => ['$in' => [self::TYPE_HOUSEHOLD, self::TYPE_STORE]]]);


        $dataProvider = new ActiveDataProvider([

            'query' => $query,

        ]);


        // load the seach form data and validate

        if (!($this->load($params) && $this->validate())) {

            return $dataProvider;

        }


        // adjust the query by adding the filters

        $query->andFilterWhere(['bid' => $this->bid])

            ->andFilterWhere(['type' => $this->type])

            ->andFilterWhere(['floor' => $this->floor]);


        // project_order is the collection name of order

        // if (isset($this->order)) {

            $query->andFilterWhere(['project_order.status' => $this->order_status]);

        // }


        return $dataProvider;

    }



Please help me. :(

Thanks so much!!!!!

Handing, searching, and sorting relations in Yii2

You have to manually set the search and sorting for relations.

Also the wiki will work for mongo as well.