如何修改CGridView列中的标题?

如何修改CGridView列中的标题?,gridview,yii,Gridview,Yii,我有一个表新闻,其中有一个类别字段,我在其中存储类别的id,还有一个相关的表新闻类别,其中有字段id和类别名称 我必须修改什么才能显示类别名称而不是id 这是我的News.php模型 class News extends CActiveRecord { public static function model($className=__CLASS__) { return parent::model($className); } /** * @return string th

我有一个表新闻,其中有一个类别字段,我在其中存储类别的id,还有一个相关的表新闻类别,其中有字段id类别名称

我必须修改什么才能显示类别名称而不是id

这是我的News.php模型

class News extends CActiveRecord
{   


public static function model($className=__CLASS__)
{
    return parent::model($className);
}

/**
 * @return string the associated database table name
 */
public function tableName()
{
    return 'news';
}


public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('title, date, description, content, category', 'required'),
        array('category', 'numerical', 'integerOnly'=>true),
        array('title, image', 'length', 'max'=>256),
        array('image','file','types'=>'jpg,jpeg,gif,png','allowEmpty'=>true),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, title, date, image, description, content, category', 'safe', 'on'=>'search'),
    );
}


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(
    'category'=>array(self::BELONGS_TO, 'NewsCategories', 'category'),
    );
}


public function attributeLabels()
{
    return array(
        'id' => '#',
        'title' => 'Title',
        'date' => 'Date',
        'image' => 'Image',
        'description' => 'Description',
        'content' => 'Content',
        'category' => 'Category',
    );
}


public function search()
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('title',$this->title,true);
    $criteria->compare('date',$this->date,true);
    $criteria->compare('image',$this->image,true);
    $criteria->compare('description',$this->description,true);
    $criteria->compare('content',$this->content,true);
    $criteria->compare('category',$this->category);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}
 }
class NewsCategories extends CActiveRecord
{

public static function model($className=__CLASS__)
{
    return parent::model($className);
}


public function tableName()
{
    return 'news_categories';
}


public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('category_name', 'required'),
        array('category_name', 'length', 'max'=>64),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, category_name', 'safe', 'on'=>'search'),
    );
}

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(
    'news'=>array(self::HAS_MANY, 'News', 'category'),
    );
}


public function attributeLabels()
{
    return array(
        'id' => 'ID',
        'category_name' => 'Название рубрики',
    );
}

/**
 * Retrieves a list of models based on the current search/filter conditions.
 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
 */
public function search()
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('category_name',$this->category_name,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}
}
还有我的NewsCategories.php模型

class News extends CActiveRecord
{   


public static function model($className=__CLASS__)
{
    return parent::model($className);
}

/**
 * @return string the associated database table name
 */
public function tableName()
{
    return 'news';
}


public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('title, date, description, content, category', 'required'),
        array('category', 'numerical', 'integerOnly'=>true),
        array('title, image', 'length', 'max'=>256),
        array('image','file','types'=>'jpg,jpeg,gif,png','allowEmpty'=>true),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, title, date, image, description, content, category', 'safe', 'on'=>'search'),
    );
}


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(
    'category'=>array(self::BELONGS_TO, 'NewsCategories', 'category'),
    );
}


public function attributeLabels()
{
    return array(
        'id' => '#',
        'title' => 'Title',
        'date' => 'Date',
        'image' => 'Image',
        'description' => 'Description',
        'content' => 'Content',
        'category' => 'Category',
    );
}


public function search()
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('title',$this->title,true);
    $criteria->compare('date',$this->date,true);
    $criteria->compare('image',$this->image,true);
    $criteria->compare('description',$this->description,true);
    $criteria->compare('content',$this->content,true);
    $criteria->compare('category',$this->category);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}
 }
class NewsCategories extends CActiveRecord
{

public static function model($className=__CLASS__)
{
    return parent::model($className);
}


public function tableName()
{
    return 'news_categories';
}


public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('category_name', 'required'),
        array('category_name', 'length', 'max'=>64),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, category_name', 'safe', 'on'=>'search'),
    );
}

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(
    'news'=>array(self::HAS_MANY, 'News', 'category'),
    );
}


public function attributeLabels()
{
    return array(
        'id' => 'ID',
        'category_name' => 'Название рубрики',
    );
}

/**
 * Retrieves a list of models based on the current search/filter conditions.
 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
 */
public function search()
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('category_name',$this->category_name,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}
}
这里是admin.php wich控制面板的和平:

 <?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'news-categories-grid',
    'dataProvider'=>$model->search(),
    //'filter'=>$model,
    'columns'=>array(
   //   'id',
       'title',
    'date',
    //'image',
    'description',
    'content',
    'category',
    array(
        'class'=>'CButtonColumn',

         ),
    ),
 ));
 ?>

您需要在新闻模型中定义关系,这样它就不会与属性名
类别
冲突:

'newsCategory'=>array(self::BELONGS_TO, 'NewsCategories', 'category'),
然后

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        'title',          // display the 'title' attribute
        'newsCategory.category_name',  // display the 'category_name' attribute of the 'category' relation
    ),
));

您需要在新闻模型中定义关系,这样它就不会与属性名称
类别
冲突:

'newsCategory'=>array(self::BELONGS_TO, 'NewsCategories', 'category'),
然后

$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
        'title',          // display the 'title' attribute
        'newsCategory.category_name',  // display the 'category_name' attribute of the 'category' relation
    ),
));

如果我需要在网格中显示来自相关模型或不同于模型属性的内容(假设您已经定义了名为
categoryModel
property,它是已创建模型的实例
newscapections
),我该怎么办

CGridView
定义的一部分:

.......
'columns'=>array(
    'title',  // standard attribute
    array( // specific data
        'name'=>'Post Category', // col title
        'value'=>function (News $model){ //dataprovider must return instance of News class
            if(!$model->category)
                return "No category";

            return $model->categoryModel->category_name;
        }
    ),
)
......
它需要php版本>=5.3。
但是它可以很容易地为旧版本重写。

如果我需要在网格中显示相关模型中的内容或与模型属性不同的内容(假设您已经定义了名为
categoryModel
property,它是创建的模型
新闻类别
的实例),那么我该怎么办

CGridView
定义的一部分:

.......
'columns'=>array(
    'title',  // standard attribute
    array( // specific data
        'name'=>'Post Category', // col title
        'value'=>function (News $model){ //dataprovider must return instance of News class
            if(!$model->category)
                return "No category";

            return $model->categoryModel->category_name;
        }
    ),
)
......
它需要php版本>=5.3。
但是可以很容易地为旧版本重写它。

在您的模型中,添加属性
categoryName
,以及函数
afterFind()

在CGridView中,使用
categoryName

'columns'=>array(
    'id',
    'title',
    'date',
    array(
        'label'=>$model->getAttributeLabel('image'),
        'type'=>'image',
        'value'=>($model->image== '') ? '' : $baseUrl.'/'.$model->image,
    ),
    'description',
    'content',
    'categoryName',
    array(
        'class'=>'CButtonColumn',
    ),
),

在您的模型中,添加属性
categoryName
,以及函数
afterFind()

在CGridView中,使用
categoryName

'columns'=>array(
    'id',
    'title',
    'date',
    array(
        'label'=>$model->getAttributeLabel('image'),
        'type'=>'image',
        'value'=>($model->image== '') ? '' : $baseUrl.'/'.$model->image,
    ),
    'description',
    'content',
    'categoryName',
    array(
        'class'=>'CButtonColumn',
    ),
),

在您的模型中,您的关系名称与属性名称之一相同,即“category”,请不要将关系名称指定为与属性名称相同的名称

将关系名称更改为类别以外的任何名称

e、 g新闻类别

那么就

'newsCategory'=>array(self::BELONGS_TO, 'NewsCategories', 'category'),
试试这个

'columns'=>array(
  'id',
  'title',
  array(
    'name' => 'category',
    'value' => '$data->newsCategory->category_name'
  ),
  .......


)

在您的模型中,您的关系名称与属性名称之一相同,即“category”,请不要将关系名称指定为与属性名称相同的名称

将关系名称更改为类别以外的任何名称

e、 g新闻类别

那么就

'newsCategory'=>array(self::BELONGS_TO, 'NewsCategories', 'category'),
试试这个

'columns'=>array(
  'id',
  'title',
  array(
    'name' => 'category',
    'value' => '$data->newsCategory->category_name'
  ),
  .......


)

它只改变栏目名称,不改变内容。请编辑您的原始问题,添加您的新闻和新闻类别模型,并查看文件代码,因为很难掌握确切的问题所在。谢谢。您的关系名称与属性“category”冲突。请参阅@Neophile answerIt仅更改栏目名称,不更改内容请编辑您的原始问题,添加您的新闻和新闻类别模型,并查看文件代码,因为很难掌握确切的问题所在。谢谢。您的关系名称与属性“category”冲突。请参见@Neophile answer另一个好方法是修改模型的搜索方法以加入引用表,这样就不需要分别延迟加载每个引用模型。另一个好方法是修改模型的搜索方法以加入引用表,这样就不需要分别延迟加载每个引用模型。