chapter 9 : Cannot show user author and comments in project list views - page 234

Hi Yiier’s,

i am still a new bie for yii, it was very intresting to learn this book. But now i’m stuck on page 234.

I want to display user comments in project list views, but there’s error like this :

PHP Error

Trying to get property of non-object

C:\xampp\htdocs\testing-yii\ch9\protected\components\views\recentComments.php(5)

01 <ul>

02 <?php foreach($this->getRecentComments() as $comment):

03 //foreach($comments as $comment): ?>

04 <div class="author">

05 <?php echo $comment->author->username; ?> added a comment.

06 </div>

07 <div class="issue">

08 <?php echo CHtml::link(CHtml::encode($comment->issue->name),

09 array(‘issue/view’, ‘id’=>$comment->issue->id)); ?>

10 </div>

11 <?php endforeach; ?>

12 </ul>

Stack Trace

#0

C:\xampp\yii\framework\web\CBaseController.php(123): require("C:\xampp\htdocs\testing-yii\ch9\protected\components\views\recen…")

#1

C:\xampp\yii\framework\web\CBaseController.php(88): CBaseController->renderInternal("C:\xampp\htdocs\testing-yii\ch9\protected\components\views\recen…", null, false)

#2

C:\xampp\yii\framework\web\widgets\CWidget.php(240): CBaseController->renderFile("C:\xampp\htdocs\testing-yii\ch9\protected\components\views\recen…", null, false)

#3

C:\xampp\htdocs\testing-yii\ch9\protected\components\RecentComments.php(31): CWidget->render("recentComments")

#4

C:\xampp\yii\framework\web\CBaseController.php(166): RecentComments->run()

#5

C:\xampp\htdocs\testing-yii\ch9\protected\views\project\index.php(19): CBaseController->widget("RecentComments")

#6

C:\xampp\yii\framework\web\CBaseController.php(119): require("C:\xampp\htdocs\testing-yii\ch9\protected\views\project\index.ph…")

#7

C:\xampp\yii\framework\web\CBaseController.php(88): CBaseController->renderInternal("C:\xampp\htdocs\testing-yii\ch9\protected\views/project\index.ph…", array("dataProvider" => CActiveDataProvider), true)

#8

C:\xampp\yii\framework\web\CController.php(866): CBaseController->renderFile("C:\xampp\htdocs\testing-yii\ch9\protected\views/project\index.ph…", array("dataProvider" => CActiveDataProvider), true)

#9

C:\xampp\yii\framework\web\CController.php(779): CController->renderPartial("index", array("dataProvider" => CActiveDataProvider), true)

#10

C:\xampp\htdocs\testing-yii\ch9\protected\controllers\ProjectController.php(149): CController->render("index", array("dataProvider" => CActiveDataProvider))

#11

C:\xampp\yii\framework\web\actions\CInlineAction.php(50): ProjectController->actionIndex()

#12

C:\xampp\yii\framework\web\CController.php(300): CInlineAction->runWithParams(array("r" => "project"))

#13

C:\xampp\yii\framework\web\filters\CFilterChain.php(133): CController->runAction(CInlineAction)

#14

C:\xampp\yii\framework\web\filters\CFilter.php(41): CFilterChain->run()

#15

C:\xampp\yii\framework\web\CController.php(1122): CFilter->filter(CFilterChain)

#16

C:\xampp\yii\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(CFilterChain)

#17

C:\xampp\yii\framework\web\filters\CFilterChain.php(130): CInlineFilter->filter(CFilterChain)

#18

C:\xampp\yii\framework\web\CController.php(283): CFilterChain->run()

#19

C:\xampp\yii\framework\web\CController.php(257): CController->runActionWithFilters(CInlineAction, array("accessControl"))

#20

C:\xampp\yii\framework\web\CWebApplication.php(328): CController->run("")

#21

C:\xampp\yii\framework\web\CWebApplication.php(121): CWebApplication->runController("project")

#22

C:\xampp\yii\framework\base\CApplication.php(155): CWebApplication->processRequest()

#23

C:\xampp\htdocs\testing-yii\ch9\index.php(13): CApplication->run()

i am using yii 1.1.7 on windows 7 home premium. and here’s i attached the listing code for model ( comment, project ), Components ( Recentcomments ) , protected\components\views\recentComments , protected\views\project\index.php. I hope my information is complete enought and your help will be appreciated.

2035

index.php

Dear All Yiier’s,

i have made comment the code for line 5 in recentComments.php file. And the error has gone, but it made the user author name did not showed in the comment’s list. IS there anybody who can help me why this command didn’t worked for recentComments.php file :

05 <?php echo $comment->author->username; ?> added a comment.

Dear All Yiier’s,

Is there anybody who can help me why the author name’s did not showed ?

Your help will be very appreciate.

Hi, check your tbl_comment table. Take a look to the issue_id. Maybe some bad index.