Tuesday, May 7, 2019

Controller Search Search Controller Query

Main Controller
===============
$query = TableStudent::find()
                    ->select('table_student.*, table_state.name AS state_name, table_state.id AS state_id, table_country.name AS country_name, table_country.id AS country_id')
                    ->innerJoin('table_state', 'table_state.id = table_student.state_id')
                    ->innerJoin('table_country', 'table_country.id = table_state.country_id')
                    ->where(['=', 'table_student.id', $id])
                    //->asArray()
                    //->all();
                    ->one();
       
        // add conditions that should always apply here
return $query;

Search Controller
=================
public function search($params)
    {
        //$query = TableStudent::find();

        /* $query = new Query;
        $query ->select([
                'table_student.id AS id',
                'table_student.name AS name',
                'table_student.email AS email',
                'table_student.address AS address',
                'table_state.name AS state_name',
                'table_country.name AS country_name',
                ]) 
                ->from('table_student')
                ->join( 'INNER JOIN', 'table_state','table_state.id = table_student.state_id')
                ->join( 'INNER JOIN', 'table_country','table_country.id = table_state.country_id'); */
       
        $query = TableStudent::find()
                    ->select('table_student.*, table_state.name AS state_name, table_country.name AS country_name')
                    ->innerJoin('table_state', 'table_state.id = table_student.state_id')
                    ->innerJoin('table_country', 'table_country.id = table_state.country_id');
       
        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

         $dataProvider->sort->attributes['country_name'] = [
            'asc' => ['table_country.name' => SORT_ASC],
            'desc' => ['table_country.name' => SORT_DESC],
        ];
       
        $dataProvider->sort->attributes['state_name'] = [
            'asc' => ['table_state.name' => SORT_ASC],
            'desc' => ['table_state.name' => SORT_DESC],
        ];
       
        $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;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'state_id' => $this->state_id,
        ]);

        $query->andFilterWhere(['like', 'table_student.name', $this->name])
            ->andFilterWhere(['like', 'table_student.email', $this->email])
            ->andFilterWhere(['like', 'table_student.address', $this->address])
            ->andFilterWhere(['like', 'table_country.name', $this->country_name])
            ->andFilterWhere(['like', 'table_state.name', $this->state_name]);

        return $dataProvider;
    }

No comments:

Post a Comment