0

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

         ),
    ),
 ));
 ?>
4

5 回答 5

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,
        ),
于 2013-09-04T16:18:01.440 回答
1

在您的模型中,您的关系名称与“类别”的属性名称之一相同,不要将关系名称与属性名称相同

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

例如新闻类别

然后相对

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

尝试这个

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


)
于 2013-09-04T12:36:39.527 回答
0

您需要在 News 模型中定义关系,以便它不会与属性名称冲突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
    ),
));
于 2013-09-03T11:38:26.030 回答
0

如果我需要显示来自相关模型或与网格中的模型属性不同的内容,请执行以下操作(假设您已经定义了News名为的属性,categoryModel它是 reated model 的实例NewsCategories

部分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。但它可以很容易地为旧版本重写。

于 2013-09-03T14:45:29.803 回答
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',
    ),
),
于 2013-09-04T02:11:02.510 回答