2018-11-12 10:22:17 1836次浏览 1条回答 0 悬赏 10 金钱

字段排序问题:按GDP倒序排列时,"中沙群岛"这条记录显示了两次,实际上应该显示为"三沙市"。最后几条记录的GDP都为0,按正序排列在同一页中时显示正常。

"中沙群岛"在第一页中显示:

a01.png

"中沙群岛"在第二页也被显示了出来,而GDP也为0的"三沙市"被忽略掉了:

a02.png

正序排列时"中沙群岛""三沙市"都能够正常显示出来:

a03.png

源代码:

namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
class CitySearch extends City
{
    public function formName(){
        return '';
    }
    public function rules()
    {     
        return [
            [['cityname','parentid', 'abbrname'], 'safe'],
        ];    
    }

    public function scenarios()
    {
        return Model::scenarios();
    }

    public function search($params)
    {
        $query = static::find()->select(['id','cityname','parentid', 'population','gdp', 'year', 'acreage', 'popu_per_sqkm', 'gdp_per_man', 'abbrname',])->with('parent');
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' =>20,
            ],
           'sort'=>[
                'attributes'=>[
                    'id',
                    'parentid',
                    'cityname',
                    'population',
                    'gdp',
                    'year',
                    'acreage',
                    'popu_per_sqkm',
                    'gdp_per_man',
                    'abbrname',
                ],
                'defaultOrder'=>['id'=>SORT_ASC],
            ],
        ]);
        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }
        $query->andFilterWhere(['=', 'parentid', $this->parentid])
            ->andFilterWhere(['like', 'cityname', $this->cityname]);
        return $dataProvider;
    }
}

请问:一条记录被显示两次的问题怎么解决?谢谢!

补充于 2018-11-14 08:46

整出来了,需要确切指定第二排序字段id的排序方式,OK

'sort'=>[
	'attributes'=>[
		'gdp'=>[
			'asc'=>['gdp'=>SORT_ASC,'id'=>SORT_ASC],
			'desc'=>['gdp'=>SORT_DESC,'id'=>SORT_DESC],
			'default'=>SORT_DESC,
		],
     'year',
     'acreage',        
     ......

感谢 的解答,谢谢!

最佳答案

  • return19931112 发布于 2018-11-12 13:07 举报
    <?php
    public function search($params)
    {
        $tableName = static::tableName();
        $query = static::find()->select([
            $tableName . '.id',
            $tableName . '.cityname',
            $tableName . '.parentid', 
            $tableName . '.population',
            $tableName . '.gdp', 
            $tableName . '.year', 
            $tableName . '.acreage', 
            $tableName . '.popu_per_sqkm', 
            $tableName . '.gdp_per_man', 
            $tableName . '.abbrname',])
            ->with('parent');
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' =>20,
            ],
           'sort'=>[
                'attributes'=>[
                    $tableName . .'id',
                    $tableName . '.parentid',
                    $tableName . '.cityname',
                    $tableName . '.population',
                    $tableName . '.gdp',
                    $tableName . '.year',
                    $tableName . '.acreage',
                    $tableName . '.popu_per_sqkm',
                    $tableName . '.gdp_per_man',
                    $tableName . '.abbrname',
                ],
                'defaultOrder'=>[$tableName . '.id'=>SORT_ASC],
            ],
        ]);
        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }
        $query->andFilterWhere(['=', $tableName . '.parentid', $this->parentid])
            ->andFilterWhere(['like', $tableName . '.cityname', $this->cityname]);
        return $dataProvider;
    }
    ?>
    

    带上表名试试

    4 条回复
    回复于 2018-11-13 14:15 回复

    兄弟,不管用啊!

    回复于 2018-11-13 15:26 回复

    echo $query->createCommand()->getRawSql()
    

    打印一下sql看看

    回复于 2018-11-13 16:11 回复

    SELECT `city`.`id`, `city`.`cityname`, `city`.`parentid`, `city`.`population`, `city`.`gdp`, `city`.`year`, `city`.`acreage`, `city`.`popu_per_sqkm`, `city`.`gdp_per_man`, `city`.`abbrname` FROM `city` WHERE `city`.`parentid` = '460000'
    
    回复于 2018-11-13 16:16 回复

    尝试把分页设置大一些,看看还会出现两条一样的记录吗

    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
阿江
副董事长

阿江

注册时间:2015-10-18
最后登录:2024-03-03
在线时长:186小时21分
  • 粉丝94
  • 金钱16816
  • 威望160
  • 积分20276

热门问题