Hello guys, this is my first post and i’m really into yii 1.1 right now. This is my first experience of learning PHP framework. so, as you can guess, i’m a beginner
for this 2 days, i’m stuck with this dreaded ‘Trying to get property of non-object’. i’d been searching for solution but every solution that came up with me can’t be implemented. (i don’t know maybe i’m still not good enough)
so, my objective is i want to display a column in CGridView when the value of one of the model table properties (named ‘Statuspelatihan’, with properties named ‘status_selesai’) is “registration”. I want to access it from model Pelatihanentry with the relations idstatuspelatihan0.
This is my relations
for model Pelatihanentry:
public function relations() {
return array(
'absens' => array(self::HAS_MANY, 'Absen', 'idpelatihanentry'),
'idpeserta0' => array(self::BELONGS_TO, 'Peserta', 'idpeserta'),
'idstatuspelatihan0' => array(self::BELONGS_TO, 'Statuspelatihan', 'idstatuspelatihan'),
'pembayarans' => array(self::HAS_MANY, 'Pembayaran', 'idpelatihanentry'),
);
}
for model statuspelatihan:
public function relations() {
return array(
'pelatihanentries' => array(self::HAS_MANY, 'Pelatihanentry', 'idstatuspelatihan'),
'idpengajar0' => array(self::BELONGS_TO, 'Pengajar', 'idpengajar'),
'idpelatihan0' => array(self::BELONGS_TO, 'Pelatihan', 'idpelatihan'),
);
}
i’m trying to accessing both model within another model’s controller called pesertacontroller.
this is the snippet of my controller action
public function actionProfil()
{
if (!Yii::app()->user->getId()){
Yii::app()->user->setFlash('gagal', "Anda bukan peserta.");
if (Yii::app()->user->hasFlash('gagal')){
echo Yii::app()->user->getFlash('gagal');
echo "</br>";
echo CHtml::link('kembali',array('site/index'));
}
//$this -> redirect(array('site/index'));
}
else {
$id=Yii::app()->user->getId();
$model = Peserta::model()->find($id);
//$model->unsetAttributes();
$entry = new Pelatihanentry;
//$entry->unsetAttributes();
if (isset($_GET['peserta'])){
$model->attributes = $_GET['peserta'];
}
/*if ($entry === null){
throw new CHttpException(404, 'The requested page does not exist.');
}*/
$this->render('profil',array('model'=>$model,'entry'=>$entry,'id'=>$id,));
}
}
and this is the view, i’m accessing the model from this (yes this is not a good practice) but when i tried to use it in controller i got that dreaded message too. this is my view
_entry.php
<?php
$columns[]=
array(
'name'=>'judul pelatihan',
'value'=>'GxHtml::valueEX($data->idstatuspelatihan0)',
);
$columns[]= array(
'name'=>'tanggal mulai',
'value'=>'$data->idstatuspelatihan0->tgl_mulai',
);
$columns[]= array(
'name'=>'tanggal selesai',
'value'=>'$data->idstatuspelatihan0->tgl_selesai',
);
//array(
//'name'=>'idstatuspelatihan',
//'value'=>'GxHtml::valueEx($data->idstatuspelatihan0)',
//'filter'=>GxHtml::listDataEx(Statuspelatihan::model()->findAllAttributes(null, true)),
//),
$columns[]=array(
'name'=>'pengajar',
'value'=>'$data->idstatuspelatihan0->idpengajar0->nama',
);
$columns[]=array(
'name'=>'status pelatihan',
'value'=>'$data->idstatuspelatihan0->status_selesai'
);
$columns[]=array(
'name'=>'nilai',
'value'=>'$data->nilai',
);
[b]//Unsolved problem 1 this is the problem of that dreaded error
[/b]
if(Pelatihanentry::model()->findByPk($id)->idstatuspelatihan0->getAttributes(array('status_selesai')) === 'registration'){
$columns[]=array(
'name'=>'Status',
'type'=>'raw',
'value'=> function($data){return 'Belum berjalan';},
);
}
else{
$columns[]=array(
'name'=>'Status',
'type'=>'raw',
'value'=> function($data){return 'Dalam proses';}, //melakukan "echo" untuk html tags pada cgridview
);
}
[b]//end of unsolved problem 1[/b]
$columns[]=array(
'class' => 'CButtonColumn',
'template'=> '{view}',
);
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'peserta-grid',
'dataProvider' => $entry->getPelatihanEntry($id),
//'filter' => $model,
'columns' => $columns,
));
}
?>
your help is very appreciated
i’m so sorry if this question is very basic. but i hope this can be my great learning experience. Thank you