src/models/search/Z1UserSearch.php
public function sqlSearch($params)
    {
        $this->load($params);

        $where =[
           'id' => sprintf('id = "%s"',$this->id),
           'status' => sprintf('status = "%s"',$this->status),
           'created_at' => sprintf('created_at = "%s"',$this->created_at),
           'updated_at' => sprintf('updated_at = "%s"',$this->updated_at),
           'username' => sprintf('username like "%s%%"',$this->username),
           'auth_key' => sprintf('auth_key like "%s%%"',$this->auth_key),
           'password_hash' => sprintf('password_hash like "%s%%"',$this->password_hash),
           'password_reset_token' => sprintf('password_reset_token like "%s%%"',$this->password_reset_token),
           'email' => sprintf('email like "%s%%"',$this->email),
        ];

        $filtedParams = array_filter($this->attributes,
            function($val){return $val!='';});

        $FiltedWhere = ['1=1'];
        foreach ($filtedParams as $key => $value) {
            $FiltedWhere[] = $where[$key];
        }

        $querySql = sprintf('
            SELECT
                %s
            FROM
                %s
            WHERE
                %s
            ', '*', $this->tableName(), implode(' AND ', $FiltedWhere));

        $countSql = sprintf('
            SELECT
                %s
            FROM
                %s
            WHERE
                %s
            ', 'count(1)', $this->tableName(), implode(' AND ', $FiltedWhere));

        $sqlDataProvider = new SqlDataProvider([
            'sql' => $querySql,
            // 'params' => [':sex' => 1],
            'totalCount' => Yii::$app->db->createCommand($countSql)->queryScalar(),
            //'sort' =>false,//如果为假则删除排序
            'key' => 'id',
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC
                ],
                'attributes' => array_keys($this->attributes),
            ],
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);

        return $sqlDataProvider;
    }
public function sqlSearch($params)
    {
        $this->load($params);

        $where =[
           'id' => sprintf('id = "%s"',$this->id),
           'status' => sprintf('status = "%s"',$this->status),
           'created_at' => sprintf('created_at = "%s"',$this->created_at),
           'updated_at' => sprintf('updated_at = "%s"',$this->updated_at),
           'username' => sprintf('username like "%s%%"',$this->username),
           'auth_key' => sprintf('auth_key like "%s%%"',$this->auth_key),
           'password_hash' => sprintf('password_hash like "%s%%"',$this->password_hash),
           'password_reset_token' => sprintf('password_reset_token like "%s%%"',$this->password_reset_token),
           'email' => sprintf('email like "%s%%"',$this->email),
        ];

        $filtedParams = array_filter($this->attributes,
            function($val){return $val!='';});

        $FiltedWhere = ['1=1'];
        foreach ($filtedParams as $key => $value) {
            $FiltedWhere[] = $where[$key];
        }

        $querySql = sprintf('
            SELECT
                %s
            FROM
                %s
            WHERE
                %s
            ', '*', $this->tableName(), implode(' AND ', $FiltedWhere));

        $countSql = sprintf('
            SELECT
                %s
            FROM
                %s
            WHERE
                %s
            ', 'count(1)', $this->tableName(), implode(' AND ', $FiltedWhere));

        $sqlDataProvider = new SqlDataProvider([
            'sql' => $querySql,
            // 'params' => [':sex' => 1],
            'totalCount' => Yii::$app->db->createCommand($countSql)->queryScalar(),
            //'sort' =>false,//如果为假则删除排序
            'key' => 'id',
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC
                ],
                'attributes' => array_keys($this->attributes),
            ],
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);

        return $sqlDataProvider;
    }
public function sqlSearch ( $ params ) { $ this -> load ( $ params ) ; $ where = [ 'id' => sprintf ( 'id = "%s"' , $ this -> id ) , 'status' => sprintf ( 'status = "%s"' , $ this -> status ) , 'created_at' => sprintf ( 'created_at = "%s"' , $ this -> created_at ) , 'updated_at' => sprintf ( 'updated_at = "%s"' , $ this -> updated_at ) , 'username' => sprintf ( 'username like "%s%%"' , $ this -> username ) , 'auth_key' => sprintf ( 'auth_key like "%s%%"' , $ this -> auth_key ) , 'password_hash' => sprintf ( 'password_hash like "%s%%"' , $ this -> password_hash ) , 'password_reset_token' => sprintf ( 'password_reset_token like "%s%%"' , $ this -> password_reset_token ) , 'email' => sprintf ( 'email like "%s%%"' , $ this -> email ) , ] ; $ filtedParams = array_filter ( $ this -> attributes , function ( $ val ) { return $ val != '' ; } ) ; $ FiltedWhere = [ '1=1' ] ; foreach ( $ filtedParams as $ key => $ value ) { $ FiltedWhere [ ] = $ where [ $ key ] ; } $ querySql = sprintf ( '
            SELECT
                %s
            FROM
                %s
            WHERE
                %s
            ' , '*' , $ this -> tableName ( ) , implode ( ' AND ' , $ FiltedWhere ) ) ; $ countSql = sprintf ( '
            SELECT
                %s
            FROM
                %s
            WHERE
                %s
            ' , 'count(1)' , $ this -> tableName ( ) , implode ( ' AND ' , $ FiltedWhere ) ) ; $ sqlDataProvider = new SqlDataProvider ( [ 'sql' => $ querySql , // 'params' => [':sex' => 1], 'totalCount' => Yii :: $ app -> db -> createCommand ( $ countSql ) -> queryScalar ( ) , //'sort' =>false,//如果为假则删除排序 'key' => 'id' , 'sort' => [ 'defaultOrder' => [ 'id' => SORT_DESC ] , 'attributes' => array_keys ( $ this -> attributes ) , ] , 'pagination' => [ 'pageSize' => 10 , ] , ] ) ; return $ sqlDataProvider ; }
Creates data provider instance with search query and sql applied

@param array $params

@return SqlDataProvider
Creates data provider instance with search query and sql applied