如何修改CGridView列中的标题?

时间:2013-09-03 11:17:23

标签: gridview yii

我有类别字段的表新闻,其中我存储了类别的ID和相关表 NewsCategories ,其中包含字段< strong> id 和 category_name

我需要修改哪些内容才能显示 category_name 而不是 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,
    ));
}
 }

我的NewsCategories.php模型

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控制面板:

 <?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',

         ),
    ),
 ));
 ?>

5 个答案:

答案 0 :(得分:2)

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'tasks',
    'dataProvider'=>$dataProvider,
    'filter'=>$task,
    'ajaxUrl'=>bu('office/view/officeId/'.$officeId),
    'columns'=>array(
        'summary',
        'due',
        array(
            'header'=>'Assigned to'
            'value'=> '$data->ownerUserName',
            'name'=> 'ownerUserName',
            'sortable'=>true,
        ),

答案 1 :(得分:1)

在您的模型中,您的关系名称与属性名称之一相同,即“类别”不会给出与属性名称相同的关系名称

将关系名称更改为类别

以外的任何名称

例如newsCategory

然后是关系

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

试试这个

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


)

答案 2 :(得分:0)

您需要在新闻模型中定义关系,以便它不会与属性名称category冲突:

'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
    ),
));

答案 3 :(得分:0)

以下是我需要在相关模型中显示某些内容或与网格中的模型属性不同的内容(假设您已定义News名为categoryModel的属性,它是reated模型的实例{{ 1}})

NewsCategories定义的一部分:

CGridView

它需要php版本&gt; = 5.3。 但它可以很容易地重写旧版本。

答案 4 :(得分:0)

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

class News extends CActiveRecord {

    public $categoryName = "";

    protected function afterFind() {
        $cat = NewsCategories::model()->findByPk($this->category);
        if(isset($cat)) $this->categoryName = $cat->category_name;

        return parent::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',
    ),
),