1

我有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

4 回答 4

3

不是

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

==>:

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

:)

于 2013-10-27T23:47:29.473 回答
1

尝试这个

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

或者

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

于 2013-10-28T10:03:15.893 回答
1

你可以试试下面:

count($data->canbothuchien)?$data->canbothuchien[0]->daXem:""
于 2013-10-28T07:08:17.877 回答
0

尝试这个

'值'=>'($data->canbothuchien != null)?$data->canbothuchien->daXem:""'

于 2015-08-03T09:42:36.623 回答