Yii2使用Sqldataprovider过滤

时间:2017-08-07 09:37:05

标签: mysql gridview yii2 csqldataprovider

我在搜索模型的search()函数中有以下代码。数据在gridview中正确显示,但我无法使用gridview的搜索功能。

public function search($params)
    {

        $totalCount = Yii::$app->db->createCommand('SELECT COUNT(*) FROM patient_test')->queryScalar();

        $query = new Query();
        $query->select('patient.id as id,patient_test.receipt_number,patient.name as patient_id, GROUP_CONCAT(test_group.name) as test_group_id, patient.age as patient_test_age')
            ->from('patient_test');
        $query->join = [
            ['LEFT JOIN', 'patient', 'patient_test.patient_id = patient.id'],
            ['LEFT JOIN', 'test_group', 'patient_test.test_group_id = test_group.id']];
        $query->groupBy(['patient_test.patient_id', 'patient_test.receipt_number', 'patient_test.is_sample_received', 'patient_test.is_ready']);


            $dataProvider = new SqlDataProvider([
//                'db' => Yii::$app->db,
                'sql' => $query->createCommand()->sql,
                'totalCount' => $totalCount,
                'sort' => [
                    'attributes' => [
                        'patient_id' => [
                            'desc' => ['patient_id' => SORT_DESC ],
                            'default' => SORT_DESC,
                        ],
                    ],
                ], 'pagination' => [
                    'pageSize' => $pagination,
                ]
            ]);


        $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,
            'is_sample_received' => $this->is_sample_received,
        ]);

        $query->andFilterWhere(['like', 'patient_test.receipt_number', $this->receipt_number])
            ->andFilterWhere(['like', 'patient.name', $this->patient_id])
            ->andFilterWhere(['like', 'test_group.name', $this->test_group_id])

->andFilterWhere(['like', 'patient.age', $this->patient_test_age])
                ->andFilterWhere(['like', 'patient.mobile_no', $this->patient_test_mobile]);


            return $dataProvider;
        }
从这里开始,我的代码工作正常,但过滤/搜索不起作用

0 个答案:

没有答案