在Yii CgridView中显示连接表中的值

时间:2014-12-10 16:38:53

标签: yii relation jointable cgridview

我在视图中有这个非常简单的表,我正试图从连接表中显示值:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'skills-grid',
    'dataProvider'=>$model->searchWithStudentSuccessRate($id),
    'template' => '{items}{pager}',
    'cssFile'=>Yii::app()->request->baseUrl. '/themes/'. Yii::app()->theme->name.'/css/table.css',
    'htmlOptions'=>array('class'=>'datagrid', 'style'=>'width:550px;'),
    'columns'=>array(
        array(
            'name'=>'name',
        ),
        array(
            'name' => 'value',
            'header' => Yii::t('MainTrans', 'Value'),
            'value' => '$data->student_skills->value',
        ),
        array(
            'name' => 'successRate',
            'header' => Yii::t('MainTrans', 'Success Rate'),
            'value' => '$data->successRate."%"',
        ),
    ),

));

这是搜索功能:

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

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('name',$this->name,true);
    $criteria->compare('t.threshold',$this->threshold);

    $criteria->with = array('student_skills');
    $criteria->together = true;

    $criteria->select = array('IFNULL(CASE WHEN ROUND((student_skills.value/t.threshold)*100,0) > 100 THEN 100 ELSE ROUND((student_skills.value/t.threshold)*100,0) END,0) as successRate','*');

    $criteria->group = "t.name";
    $criteria->condition = 'student_skills.student_id = '.$id;

    $criteria->compare('successRate',$this->successRate);
    $criteria->compare('student_skills.value',$this->value);


    return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'pagination'=>array(
                    'pageSize'=>25,
            ),
            'sort'=>array(
                    'defaultOrder'=>array(
                            'successRate'=>CSort::SORT_DESC,
                    ),
                    'attributes'=>array(
                            'successRate'=>array(
                                    'asc'=>'successRate',
                                    'desc'=>'successRate DESC',
                            ),
                            '*',
                    ),
            ),
    ));
}

但是当我将value列添加到我的CGridView时,我收到错误:“试图获取非对象的属性”。

一切正常,没有列值(列successRatename都很好)。关系也应该没问题。我得到的错误意味着该值不存在,但它应该是因为我在其他视图中做了类似的事情并且它有效。

有人能说出什么问题吗?我确定这是一个小问题,但我一直在努力解决这个尴尬问题。

由于

1 个答案:

答案 0 :(得分:0)

这意味着在某些情况下$data->student_skillsNULL。尝试改变这个:

'value' => '$data->student_skills->value',

到这个

'value' => 'empty($data->student_skills) ? null : $data->student_skills->value',
相关问题