Add comments in post yii

烈酒焚心 提交于 2020-01-06 07:29:07

问题


I am trying to add comment module in my blogging website . What I have done is :

No 1 Add a <div> in Blog post view.php to render tblcomments/_form

<?php 

        $model_comments = new TblComments;

   $this->renderPartial('/TblComments/_form',array(
            'comments'=>$model_comments,
        ));

 ?>

No 2 : This is my TblComments/_form.php

<

div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'tbl-comments-form',
    'enableAjaxValidation'=>false,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model_comments); ?>

    <div class="row">
        <?php // echo $form->labelEx($model,'user_id'); ?>
        <?php echo $form->hiddenField($model_comments,'user_id'); ?>
        <?php echo $form->error($model_comments,'user_id'); ?>
    </div>

    <div class="row">
        <?php // echo $form->labelEx($model,'post_id'); ?>
        <?php echo $form->hiddenField($model_comments,'post_id'); ?>
        <?php echo $form->error($model_comments,'post_id'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model_comments,'comment_body'); ?>
        <?php echo $form->textArea($model_comments,'comment_body',array('rows'=>5,'cols'=>35)); ?>
        <?php echo $form->error($model_comments,'comment_body'); ?>
    </div>
    <div class="row buttons">
        <?php echo CHtml::submitButton($model_comments->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

Problem is that :

Undefined variable: model_comments 

P.S : And this error is occurring on TblComments/_form file on line :

<?php echo $form->errorSummary($model_comments); ?>

Can anyone explain me why this undefined as I have already defined it !


回答1:


A minor mistake this is, when you do:

$this->renderPartial('/TblComments/_form',array(
    'comments'=>$model_comments,
));
// or even if you are using render()

The view that is passed the model instance gets it as $comments and not $model_comments, meaning if you do:

$this->render('someview', array('model_there'=>$model_here));

The view has to use $model_there and not $model_here. As said in the guide:

the render() method will extract the second array parameter into variables. As a result, in the view script we can access the local variables $var1 and $var2.


That said you should move the instance creation to the controller and then pass it to your view:

// controller action
public function actionActionname($id){
    $model_here = new TblComments;
    $postmodel = loadModel($id);
    // ...
    $this->render('view', array(
        'postmodel'=>$postmodel,
        'model_there'=>$model_here
    ));
}

// in view.php
$this->renderPartial('/TblComments/_form', array(
    'model_there'=>$model_there
);

// then in _form you use $model_there
<?php echo $form->errorSummary($model_there); ?>



回答2:


I simply add

  $model_comments =  new TblComments();

in TblComments/_form.php



来源:https://stackoverflow.com/questions/13152152/add-comments-in-post-yii

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!