在帖子中添加评论 yii [英] Add comments in post yii

查看:27
本文介绍了在帖子中添加评论 yii的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的博客网站中添加评论模块.我所做的是:

No 1 在博客文章view.php中添加

来渲染tblcomments/_form

renderPartial('/TblComments/_form',array('评论'=>$model_comments,));?>

No 2:这是我的 TblComments/_form.php

<

div class="form"><?php $form=$this->beginWidget('CActiveForm', array('id'=>'tbl-comments-form','enableAjaxValidation'=>false,));?><p class="note">带有 <span class="required">*</span> 的字段</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 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 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 class="行按钮"><?php echo CHtml::submitButton($model_comments->isNewRecord ? 'Create' : 'Save');?>

<?php $this->endWidget();?></div><!-- 形式-->

问题在于:

未定义变量:model_comments

P.S:此错误发生在 TblComments/_form 文件上:

errorSummary($model_comments);?>

谁能解释一下为什么这个未定义,因为我已经定义了它!

解决方案

一个小错误,当你这样做时:

$this->renderPartial('/TblComments/_form',array('评论'=>$model_comments,));//或者即使你正在使用 render()

传递给模型实例的视图将其作为 $comments 而不是 $model_comments,这意味着如果您这样做:

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

视图必须使用 $model_there 而不是 $model_here.正如 指南:

<块引用>

render() 方法会将第二个数组参数提取到变量中.因此,在视图脚本中我们可以访问局部变量 $var1 和 $var2.

<小时>

也就是说,您应该将实例创建移动到控制器,然后将其传递给您的视图:

//控制器动作公共函数 actionActionname($id){$model_here = 新 TblComments;$postmodel = loadModel($id);//...$this->render('view', array('postmodel'=>$postmodel,'model_there'=>$model_here));}//在 vi​​ew.php 中$this->renderPartial('/TblComments/_form', array('model_there'=>$model_there);//然后在 _form 中使用 $model_there<?php echo $form->errorSummary($model_there);?>

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 !

解决方案

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); ?>

这篇关于在帖子中添加评论 yii的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆