Dreaded 'Trying to get property of non-object'

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

‘Trying to get property of non-object’. ??

for which object u get this error. kindly attach screenshot of your error so it would be easy to find error line.

this one in the attachment

this happen whenever i’m trying to access statuspelatihan (model/table) from pelatihanentry (model/table) with relations from pelatihanentry, idstatuspelatihan0.

this one


//Unsolved problem 1 this is the problem of that dreaded error


        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

                        );

        }

//end of unsolved problem 1

When using a relation like "idstatuspelatihan0" if there are no related records you get NULL and if your code does not check for that you get the famous "non-object" error.

For example if you use


$a = $model->idstatuspelatihan0->id

in case that the relation returns NULL you will get that error message.

Instead you need to use code like


$a = isset($model->idstatuspelatihan0) ? $model->idstatuspelatihan0->id : 'n/a';

For more info check this section from the guide - http://www.yiiframew…elational-query