我不能在CGRIDVIEW中用HAS_MANY关系显示价值(Yii)

时间:2013-10-27 14:53:54

标签: yii has-many

我有2张桌子: “tblcongvanden”(ID,tencongvan)(1)-------< - (n)“tblcanbothuchien”(ID,maCongVan(FK),maCanBo,daXem)

在congvanden的模型(tblcongvanden表)中,我有一个关系:

public function relations() {
    return array(
        'canbothuchien' => array(self::HAS_MANY,'canbothuchien','maCongVan'),
        'mucdomat' => array(self::BELONGS_TO,'mucdomat','maMucDoMat'),
    );
}

我有一个功能

public function dsCVDchuaXem(){
    $userid = canbo::model()->find('tenDangNhap=:ten',array(':ten'=>Yii::app()->user->id))->ID;

    $criteria = new CDbCriteria;
    $criteria->with= array('canbothuchien','mucdomat');
    $criteria->condition = 'maCanBo=:ma';
    $criteria->params= array(':ma'=>$userid);

    $criteria->compare('t.ID',$this->ID);
    $criteria->compare('tieuDe',$this->tieuDe,true);
    $criteria->compare('soDen',$this->soDen,true);

    $rev=$this->ngayDen;
    $rev = preg_replace('/^(\d{1,2})-(\d{1,2})-(\d{2,4})$/',"$3-$2-$1",$rev); //for day-month-year
    $criteria->compare('ngayDen', $rev,true);

    $rev=$this->ngayThang;
    $rev = preg_replace('/^(\d{1,2})-(\d{1,2})-(\d{2,4})$/',"$3-$2-$1",$rev); //for day-month-year
    $criteria->compare('ngayThang', $rev,true);

    $criteria->compare('maMucDoMat',$this->maMucDoMat);
    $criteria->together = true;

    return new CActiveDataProvider($this, array(
        'pagination'=> array(
            'pageSize'=> 10
        ),
        'criteria'=>$criteria,
    ));
}

在index.php中,如何在Cgridview中显示字段“daXem”?

(我输入了'value'=>'$data->canbothuchien[0]->daXem',错误是“试图获取非对象的属性”,或者如果我输入"value'=>'$data->canbothuchien->daXem"那也是错误的

4 个答案:

答案 0 :(得分:3)

'value'=>'$data->canbothuchien[0]->daXem' 

==> :

'value'=>'$data->canbothuchien->daXem' 

:)

答案 1 :(得分:1)

你可以尝试下面的内容:

count($data->canbothuchien)?$data->canbothuchien[0]->daXem:""

答案 2 :(得分:1)

试试这个

array (
         'header'=>'daXem',
         'value'=>function($data) {
            return (!empty($data->canbothuchien->daXem)) ? $data->canbothuchien->daXem : '';
                }
    ),

导致 aferFind() beforeFind()挂钩的问题,如果您创建了这些挂钩,则可以重新检查。

答案 3 :(得分:0)

试试这个

' value' =>'($ data-> canbothuchien!= null)?$ data-> canbothuchien-> daXem:""& #39;