Ch 6 Pag 140 - Trying to get property of non-object


After implementing the code of ch6 page 140 i’m having this error and was not able to find what’s wrong. Here’s the code:

from protected/views/issue/view.php:







	array('label'=>'List Issue', 'url'=>array('index')),

	array('label'=>'Create Issue', 'url'=>array('create')),

	array('label'=>'Update Issue', 'url'=>array('update', 'id'=>$model->id)),

	array('label'=>'Delete Issue', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),

	array('label'=>'Manage Issue', 'url'=>array('admin')),



<h1>View Issue #<?php echo $model->id; ?></h1>

<?php $this->widget('zii.widgets.CDetailView', array(























)); ?>

the relationship method of my issue model:

	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(

			'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),

			'project' => array(self::BELONGS_TO, 'Project', 'project_id'),

			'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),



and the stack trace:

PHP Error

Trying to get property of non-object


22 ‘name’,

23 ‘description’,

24 array(

25 ‘name’=>‘type_id’,

26 ‘value’=>CHtml::encode($model->getTypeText())

27 ),

28 array(

29 ‘name’=>‘status_id’,

30 ‘value’=>CHtml::encode($model->getStatusText())

31 ),

32 array(

33 ‘name’=>‘owner_id’,

34 ‘value’=>CHtml::encode($model->owner->username)

35 ),

36 array(

37 ‘name’=>‘requester_id’,

38 ‘value’=>CHtml::encode($model->requester->username)

39 ),

40 ),

41 )); ?>

Stack Trace


C:\yii\site\cgi-bin\libs\yii\framework\web\CBaseController.php(119): require("C:\yii\site\htt…")


C:\yii\site\cgi-bin\libs\yii\framework\web\CBaseController.php(88): CBaseController->renderInternal("C:\yii\site\htt…", array("model" => Issue), true)


C:\yii\site\cgi-bin\libs\yii\framework\web\CController.php(866): CBaseController->renderFile("C:\yii\site\htt…", array("model" => Issue), true)


C:\yii\site\cgi-bin\libs\yii\framework\web\CController.php(779): CController->renderPartial("view", array("model" => Issue), true)


C:\yii\site\httpdocs\trackstar\protected\controllers\IssueController.php(61): CController->render("view", array("model" => Issue))

56 */

57 public function actionView($id)

58 {

59 $this->render(‘view’,array(

60 ‘model’=>$this->loadModel($id),

61 ));

62 }


64 /**

65 * Creates a new model.

66 * If creation is successful, the browser will be redirected to the ‘view’ page.


unknown(0): IssueController->actionView("1")


C:\yii\site\cgi-bin\libs\yii\framework\web\actions\CAction.php(104): ReflectionMethod->invokeArgs(IssueController, array("1"))


C:\yii\site\cgi-bin\libs\yii\framework\web\actions\CInlineAction.php(48): CAction->runWithParamsInternal(IssueController, ReflectionMethod, array("r" => "issue/view", "id" => "1"))


C:\yii\site\cgi-bin\libs\yii\framework\web\CController.php(300): CInlineAction->runWithParams(array("r" => "issue/view", "id" => "1"))


C:\yii\site\cgi-bin\libs\yii\framework\web\filters\CFilterChain.php(133): CController->runAction(CInlineAction)


C:\yii\site\cgi-bin\libs\yii\framework\web\filters\CFilter.php(41): CFilterChain->run()


C:\yii\site\cgi-bin\libs\yii\framework\web\CController.php(1122): CFilter->filter(CFilterChain)


C:\yii\site\cgi-bin\libs\yii\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)


C:\yii\site\cgi-bin\libs\yii\framework\web\filters\CFilterChain.php(130): CInlineFilter->filter(CFilterChain)


C:\yii\site\cgi-bin\libs\yii\framework\web\CController.php(283): CFilterChain->run()


C:\yii\site\cgi-bin\libs\yii\framework\web\CController.php(257): CController->runActionWithFilters(CInlineAction, array("accessControl", "projectContext + create"))


C:\yii\site\cgi-bin\libs\yii\framework\web\CWebApplication.php(328): CController->run("view")


C:\yii\site\cgi-bin\libs\yii\framework\web\CWebApplication.php(121): CWebApplication->runController("issue/view")


C:\yii\site\cgi-bin\libs\yii\framework\base\CApplication.php(155): CWebApplication->processRequest()


C:\yii\site\httpdocs\trackstar\index.php(13): CApplication->run()

08 defined(‘YII_DEBUG’) or define(‘YII_DEBUG’,true);

09 // specify how many levels of call stack should be shown in each log message

10 defined(‘YII_TRACE_LEVEL’) or define(‘YII_TRACE_LEVEL’,3);


12 require_once($yii);

13 Yii::createWebApplication($config)->run();

2011-06-19 14:37:09 Apache/2.2.19 (Win32) Yii Framework/1.1.7

What is the value of the "owner_id" field in the database for the issue you are trying to view? i.e., what is the response from mysql from this sql:

[sql]SELECT owner_id FROM tbl_issue WHERE id = 1[/sql]

If the owner_id in the issue row does not match a valid id in the tbl_user table, then the related User would not be able to be established and would result in this error.

That was the problem: neither the ownner_id nor the requester_id were set in the bd data.

What is the best way to deal with this kind of errors? Create a public method on the model class to check if the issue->owner and issue->requester are set and return a “user does not exist” msg if not and call that method on the view? Can it be done right in the view putting that verification somehow on the “‘value’=> CHtml::encode($model->owner->username)”?

Thanks for the help and for a great book!

Nuno Mendes is right to see working of this you should insert related owner_id and requester_id through SQL query or PHPMyadmin UI.