yiicgridviewrelated-content

Yii : Error 500 Trying to get property of non-object


I have problem , always get Error 500 "Trying to get property of non Object" while i try to show Notrans_FPB in my Cgridview, can anyone help me ? whats wrong with my code , Thanks ..

This is my Controller "DafTimbangBahan"

    public function actionAdmin()
{
    $model=new DafTimbangBahan('search');
    $dafFpbs=new DafFpb;

    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['DafTimbangBahan']))
        $model->attributes=$_GET['DafTimbangBahan'];

    $this->render('admin',array(
        'model'=>$model,
    ));
}

My Model DaftimbangBahan

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(
        'dafBpbs' => array(self::HAS_MANY, 'DafBpb', 'Notrans'),
        'dafFpbs' => array(self::HAS_MANY, 'DafFpb', 'Notrans'),
        'koSup' => array(self::BELONGS_TO, 'Supplier', 'KoSup'),
        'koCab' => array(self::BELONGS_TO, 'Cabang2', 'KoCab'),
        'bahans' => array(self::MANY_MANY, 'Bahan', 'det_timbang_bahan(Notrans, KoHan)'),
        'notransTimbangs' => array(self::HAS_MANY, 'NotransTimbang', 'Notrans'),
    );
}

and This is my View File

    <?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'daf-timbang-bahan-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(   
    array(
    'name'=>'dafFpbs.Notrans_FPB',
    'header'=>'No. FPB',
    'value'=>'$data->dafFpbs->Notrans_FPB',
    ),
    'NoTrans',
    array(
        'class'=>'CButtonColumn',
    ),
),

Solution

  • I think your relations structure is incorrect. You have 'dafFpbs' => array(self::HAS_MANY, 'DafFpb', 'Notrans') in your DafTimbangBahan model. This means every DafTimbangBahan record has many Notrans record. So $data->dafFpbs will be an array, not an object. Therefore $data->dafFpbs->Notrans_FPB causes "Trying to get property of non object" error. For solving this problem you should use 'value'=>'$data->dafFpbs[0]->Notrans_FPB'(0 is an example) isntead.