错误的CSqlDataProvider排序属性文本


Wrong CSqlDataProvider sorting attributes text

我使用的是Yii 1.1.15,在我的ClistView中,我有一些可排序的属性。这很好,问题是测试没有从我的模型类中的attributeLabels()中读取。

$dataProvider=new CSqlDataProvider($sql, array(
        'params'=> array(':lat' => $lat, ':lng'=>$lng, ':radius'=>$radius),
        'totalItemCount'=>$count,
        'sort'=>array(
            'defaultOrder'=>'distance ASC',
            'attributes'=>array(
                'store','state','distance'
            ),
        ),
        'pagination'=>array(
            'pageSize'=> $perPage,
        ),
    )
);

我也必须把它改到下面才能工作。

'attributes' => array(
    'store'=>'Store Name', 
    'state'=> 'State',
    'distance'=>'Distance to Store'
),

知道我错过了什么吗?

这是我型号中的attributeLabels()

public function attributeLabels(){
    return array(
        'id' => 'ID',
        'store' => 'Store Name',
        'address' => 'Address',
        'city' => 'City',
        'state' => 'State',
        'country' => 'Country',
        'descscription' => 'Description',
        'ip' => 'Ip',
        'distance'=>'Distance to Store'
    );
}

以下是我在store.php模型中对state模型的关系

public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            '_state' => array(self::BELONGS_TO, 'state', 'state'),
            '_contact' => array(self::HAS_MANY, 'StoreContact', 'store_id'),
        );
    }

CActiveDataProviderCSqlDataProvider之间存在巨大差异:

CActiveDataProvider

CActiveDataProvider实现了一个基于ActiveRecord的数据提供程序。

CSqlDataProvider

CSqlDataProvider实现了一个基于纯SQL语句的数据提供程序。

所以,当您使用CSqlDataProvider时,模型将没有角色,您的数据提供者也不会查找任何模型。

但是,如果您想使用模型属性,可以在CSqlDataProvider中使用键值数组,并执行以下操作:

'attributes' => array(
    'store'=> StoreModel::model()->getAttributeLabel('store'), 
    'state'=> StoreModel::model()->getAttributeLabel('state'),
    'distance'=> StoreModel::model()->getAttributeLabel('distance'),
),

请注意,我假设您的模型名称为StoreModel