[SOLVED] Chapter 9 Comment input form issue Page 223

Hi,

I’m using Yii 1.1.2 and on page 223 stucked on displaying comment input form. Here is the error witch I receiving:


PHP Error


Description


Undefined variable: comment


Source File


/var/www/trackstar/protected/views/issue/view.php(63)


00051:         'comments'=>$model->comments,

00052:     )); ?>

00053:     <?php endif; ?>

00054: 

00055:     <h3>Leave a Comment</h3>

00056: 

00057:     <?php if(Yii::app()->user->hasFlash('commentSubmitted')): ?>

00058:     <div class="flash-success">

00059:         <?php echo Yii::app()->user->getFlash('commentSubmitted'); ?>

00060:     </div>

00061: 

00062:     <?php else: ?>

00063:         <?php $this->renderPartial('/comment/_form', array('model' => $comment)); ?>

00064: 

00065:    <?php endif; ?>

00066:     

00067: </div>

Stack Trace


#0 /var/www/yii/framework/web/CBaseController.php(119): require()

#1 /var/www/yii/framework/web/CBaseController.php(88): IssueController->renderInternal()

#2 /var/www/yii/framework/web/CController.php(748): IssueController->renderFile()

#3 /var/www/yii/framework/web/CController.php(687): IssueController->renderPartial()

#4 /var/www/trackstar/protected/controllers/IssueController.php(68): IssueController->render()

#5 /var/www/yii/framework/web/actions/CInlineAction.php(32): IssueController->actionView()

#6 /var/www/yii/framework/web/CController.php(300): CInlineAction->run()

#7 /var/www/yii/framework/web/filters/CFilterChain.php(129): IssueController->runAction()

#8 /var/www/yii/framework/web/filters/CFilter.php(41): CFilterChain->run()

#9 /var/www/yii/framework/web/CController.php(999): CAccessControlFilter->filter()

#10 /var/www/yii/framework/web/filters/CInlineFilter.php(59): IssueController->filterAccessControl()

#11 /var/www/yii/framework/web/filters/CFilterChain.php(126): CInlineFilter->filter()

#12 /var/www/yii/framework/web/CController.php(283): CFilterChain->run()

#13 /var/www/yii/framework/web/CController.php(257): IssueController->runActionWithFilters()

#14 /var/www/yii/framework/web/CWebApplication.php(320): IssueController->run()

#15 /var/www/yii/framework/web/CWebApplication.php(120): CWebApplication->runController()

#16 /var/www/yii/framework/base/CApplication.php(135): CWebApplication->processRequest()

#17 /var/www/trackstar/index.php(12): CWebApplication->run()

2011-01-04 07:37:39 Apache/2.2.14 (Ubuntu) Yii Framework/1.1.2

Here is the content of /protected/issue/view.php file:


<?php

$this->breadcrumbs=array(

	'Issues'=>array('index'),

	$model->name,

);


$this->menu=array(

	array('label'=>'List Issue', 'url'=>array('index', 'pid'=>$model->project->id)),

	array('label'=>'Create Issue', 'url'=>array('create', 'pid'=>$model->project->id)),

	array('label'=>'Update Issue', 'url'=>array('update', 'id'=>$model->id, 'pid'=>$model->project->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', 'pid'=>$model->project->id)),

);

?>


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


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

	'data'=>$model,

	'attributes'=>array(

		'id',

		'name',

		'description',

		array(

                    'name'=>'type_id',

                    'value'=>  CHtml::encode($model->getTypeText())

                ),

		array(

                    'name'=>'status_id',

                    'value'=> CHtml::encode($model->getStatusText())

                ),

		array(

                    'name'=>'owner_id',

                    'value'=>  CHtml::encode($model->owner->username),

                ),

		array(

                    'name'=>'requester_id',

                    'value'=>  CHtml::encode($model->owner->username),

                ),

		

	),

)); ?>


<div id="comments">

    <?php if($model->commentCount >= 1): ?>

    <h3>

        <?php echo $model->commentCount > 1 ? $model->commentCount . ' comments' : 'One comment'; ?>

    </h3>


    <?php $this->renderPartial('_comments', array(

        'comments'=>$model->comments,

    )); ?>

    <?php endif; ?>


    <h3>Leave a Comment</h3>


    <?php if(Yii::app()->user->hasFlash('commentSubmitted')): ?>

    <div class="flash-success">

        <?php echo Yii::app()->user->getFlash('commentSubmitted'); ?>

    </div>


    <?php else: ?>

        <?php $this->renderPartial('/comment/_form', array('model' => $comment)); ?>


   <?php endif; ?>

    

</div>



I checked my code against the book code and didn’t find any errors. If anyone have idea it wolud be appreciated.

Your error message says Undefined variable: comment, and this means exactly what it says. Look carefully to your code and you will see that you are sending $comment variable (not $comments, but $comment) to renderPartial method, but you don’t have this variable defined nor you don’t assign any value to it.

If you are 100% sure that you didn’t remove line that defines or assigns any value to this variable then it seems that book has an error.

Thanks for your quick reply.

I made a mistake. After I thoroughly check code again find that I didn’t pass comment variable to view in actionView method


public function actionView()

	{

            $issue = $this->loadModel();

            $comment = $this->createComment($issue);

            

		$this->render('view',array(

			'model'=>$issue,

                        'comment'=>$comment,

		));

	}

Thanks again for your effort.

No, problem! :] Always a pleasure to help! :}

Kudos - I got caught in the same trap. I believe the book should have that line highlighted to make it stand out more :)

Thanks. This helped me too! The problem was on pg. 220 the book shows changes in BOLD. However adding ‘comment’=>$comment, was not it bold and easily missed!

thanks. I had the same prob. This book needs updating