我有一个带有类别字段的表News ,其中存储了类别的 id 和一个相关的表NewsCategories,其中包含字段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',
),
),
));
?>