Hi,
I am having some trouble with Chapter 9 of the book. Instead of getting the comments form on the issue listing page, I get a PHP notice: Trying to get property of non-object.
Here is the complete stacktrace:
PHP notice
Trying to get property of non-object
/home/deridder/NetBeansProjects/trackstar/protected/views/issue/_comments.php(5)
01 <?php foreach($comments as $comment): ?>
02 <div class="comment">
03 <div class="author">
04
05 <?php echo $comment->author->username; ?>:
06 </div>
07
08 <div class="time">
09 on <?php echo date('F j, Y \a\t h:i:a', strtotime($comment->create_time)); ?>
10 </div>
11
12 <div class="content">
13 <?php echo nl2br(CHtml::encode($comment->content)); ?>
14 </div>
15 <hr>
16
17 </div> <!-- comment -->
Stack Trace
#0
+
/home/deridder/yii/framework/web/CBaseController.php(127): require("/home/deridder/NetBeansProjects/trackstar/protected/views/issue/...")
#1
+
/home/deridder/yii/framework/web/CBaseController.php(96): CBaseController->renderInternal("/home/deridder/NetBeansProjects/trackstar/protected/views/issue/...", array("comments" => array(Comment, Comment, Comment, Comment)), true)
#2
+
/home/deridder/yii/framework/web/CController.php(870): CBaseController->renderFile("/home/deridder/NetBeansProjects/trackstar/protected/views/issue/...", array("comments" => array(Comment, Comment, Comment, Comment)), true)
#3
–
/home/deridder/NetBeansProjects/trackstar/protected/views/issue/view.php(40): CController->renderPartial("_comments", array("comments" => array(Comment, Comment, Comment, Comment)))
35 <?php if($model->commentCount>=1): ?>
36 <h3>
37 <?php echo $model->commentCount>1 ? $model->commentCount.'comments' : 'One comment'; ?>
38 </h3>
39
40 <?php $this->renderPartial('_comments', array('comments'=>$model->comments,)); ?>
41 <?php endif; ?>
42
43 <h3>Leave a comment</h3>
44
45 <?php if (Yii::app()->user->hasFlash('commentSubmitted')): ?>
#4
+
/home/deridder/yii/framework/web/CBaseController.php(127): require("/home/deridder/NetBeansProjects/trackstar/protected/views/issue/...")
#5
+
/home/deridder/yii/framework/web/CBaseController.php(96): CBaseController->renderInternal("/home/deridder/NetBeansProjects/trackstar/protected/views/issue/...", array("model" => Issue, "comment" => Comment), true)
#6
+
/home/deridder/yii/framework/web/CController.php(870): CBaseController->renderFile("/home/deridder/NetBeansProjects/trackstar/protected/views/issue/...", array("model" => Issue, "comment" => Comment), true)
#7
+
/home/deridder/yii/framework/web/CController.php(783): CController->renderPartial("view", array("model" => Issue, "comment" => Comment), true)
#8
–
/home/deridder/NetBeansProjects/trackstar/protected/controllers/IssueController.php(111): CController->render("view", array("model" => Issue, "comment" => Comment))
106 #));
107
108 # changes made on page 220
109 $issue = $this->loadModel($id);
110 $comment = $this->createComment($issue);
111 $this->render('view', array('model'=>$issue, 'comment'=>$comment,));
112 }
113
114 /**
115 * Creates a new model.
116 * If creation is successful, the browser will be redirected to the 'view' page.
#9
unknown(0): IssueController->actionView("1")
#10
+
/home/deridder/yii/framework/web/actions/CAction.php(107): ReflectionMethod->invokeArgs(IssueController, array("1"))
#11
+
/home/deridder/yii/framework/web/actions/CInlineAction.php(48): CAction->runWithParamsInternal(IssueController, ReflectionMethod, array("r" => "issue/view", "id" => "1"))
#12
+
/home/deridder/yii/framework/web/CController.php(309): CInlineAction->runWithParams(array("r" => "issue/view", "id" => "1"))
#13
+
/home/deridder/yii/framework/web/filters/CFilterChain.php(134): CController->runAction(CInlineAction)
#14
+
/home/deridder/yii/framework/web/filters/CFilter.php(41): CFilterChain->run()
#15
+
/home/deridder/yii/framework/web/CController.php(1146): CFilter->filter(CFilterChain)
#16
+
/home/deridder/yii/framework/web/filters/CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)
#17
+
/home/deridder/yii/framework/web/filters/CFilterChain.php(131): CInlineFilter->filter(CFilterChain)
#18
+
/home/deridder/yii/framework/web/CController.php(292): CFilterChain->run()
#19
+
/home/deridder/yii/framework/web/CController.php(266): CController->runActionWithFilters(CInlineAction, array("accessControl", "projectContext + create index admin"))
#20
+
/home/deridder/yii/framework/web/CWebApplication.php(276): CController->run("view")
#21
+
/home/deridder/yii/framework/web/CWebApplication.php(135): CWebApplication->runController("issue/view")
#22
+
/home/deridder/yii/framework/base/CApplication.php(162): CWebApplication->processRequest()
#23
–
/home/deridder/NetBeansProjects/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);
11
12 require_once($yii);
13 Yii::createWebApplication($config)->run();
These are the code snippets that have been altered or added in this chapter:
In IssueController.php :
public function actionView($id)
{
#$this->render('view',array(
# 'model'=>$this->loadModel($id),
#));
# changes made on page 220
$issue = $this->loadModel($id);
$comment = $this->createComment($issue);
$this->render('view', array('model'=>$issue, 'comment'=>$comment,));
}
Also in IssueController.php :
protected function createComment($issue) {
$comment = new Comment;
if(isset($_POST['Comment'])) {
$comment->attributes = $_POST['Comment'];
if($issue->addComment($comment)) {
Yii::app()->user->setFlash('commentSubmitted', "Your comment has been added. ");
$this->refresh();
}
}
return $comment;
}
The file _comments.php
<?php foreach($comments as $comment): ?>
<div class="comment">
<div class="author">
<?php echo $comment->author->username; ?>:
</div>
<div class="time">
on <?php echo date('F j, Y \a\t h:i:a', strtotime($comment->create_time)); ?>
</div>
<div class="content">
<?php echo nl2br(CHtml::encode($comment->content)); ?>
</div>
<hr>
</div> <!-- comment -->
<?php endforeach; ?>
I have checked for typo’s, but I can’t find any myself, maybe someone else can.
Thanks,
Edwin