Explain search model please

Hey everyone,

I’m just hoping someone could help me understand the search model generated by gii. I’ve been using it and modifying it just fine, but I was combing through and noticed something I couldn’t make sense of. This is more of a syntax thing than anything else.


    public function search($params)

    {

        $query = User::find()

                ->select(['username']);


        // add conditions that should always apply here


        $dataProvider = new ActiveDataProvider([

            'query' => $query,

        ]);


        $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(['like', 'username', $this->username])

            ->andFilterWhere(['not like', 'username', Yii::$app->user->identity->username]);




        return $dataProvider;

    }

Data provider is initiated early in the function, using the original query. However, the query is still being modified later in the function. How is it that the data provider still reflects the later modifications to query without being updated after the newer query conditions?

I’ve been using this structure but never really looked that closely at it until now. Really just curious about it.

Thanks.

Query is an object. All objects are PHP are passed by reference instead of being copied. Unless you use "clone", of course.