Default sort on related table

Hi,

Below is my portion of Search model:




    /**

     * Creates data provider instance with search query applied

     *

     * @param array $params

     *

     * @return ActiveDataProvider

     */

    public function search($params) {

        $this->load($params);


        $query = SubconWork::find();

        if (Budget::BLOK_I == $this->groupProject) {

            $query->joinWith([

                'project' =>

                function($query) {

                    return $query->from(gdgi\Partner::getFullTableName() . ' proj');

                },

                'provider' =>

                function($query) {

                    return $query->from(gdgi\Partner::getFullTableName() . ' prov');

                }]);

        } else {

            $query->joinWith([

                'project' =>

                function($query) {

                    return $query->from(Partner::getFullTableName() . ' proj');

                },

                'provider' =>

                function($query) {

                    return $query->from(Partner::getFullTableName() . ' prov');

                }]);

        }


        $dataProvider = new ActiveDataProvider([

            'query' => $query,

            'pagination' => [

                'pageSize' => 10,

            ],

            'sort' => [

                'defaultOrder' => [

                    'date' => SORT_DESC,

                    'projectFk' => SORT_ASC,

                ]

            ],

        ]);


        $dataProvider->sort->attributes['projectFk'] = [

            'asc' => ['proj.name' => SORT_ASC],

            'desc' => ['proj.name' => SORT_DESC],

        ];

        $dataProvider->sort->attributes['providerFk'] = [

            'asc' => ['prov.name' => SORT_ASC],

            'desc' => ['prov.name' => SORT_DESC],

        ];


        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' => $this->id,

            'date' => $this->date,

            'groupProject' => $this->groupProject,

//            'projectFk' => $this->projectFk,

//            'providerFk' => $this->providerFk,

            'status' => $this->status,

        ]);


        $query->andFilterWhere(['like', 'description', $this->description])

                ->andFilterWhere(['like', 'memo', $this->memo]);


        $query->andFilterWhere(['like', 'proj.name', $this->projectFk]);

        $query->andFilterWhere(['like', 'prov.name', $this->providerFk]);


        return $dataProvider;

    }



My primary question is how can I sort according to project.name? I can successfully sort and filter according to its project name, but when I change to proj.name it gave me error:




	exception 'yii\base\ErrorException' with message 'Undefined index: proj.name' in .../vendor/yiisoft/yii2/data/Sort.php:225



The changed code




        $dataProvider = new ActiveDataProvider([

            'query' => $query,

            'pagination' => [

                'pageSize' => 10,

            ],

            'sort' => [

                'defaultOrder' => [

                    'date' => SORT_DESC,

                    'proj.name' => SORT_ASC,

                ]

            ],

        ]);



Any help?

PS:

My other question is, my related tables are on two database, it links to related table according to its groupProject attribute. Any better way to specify it?