yii博客评论模型与多模式形式

时间:2011-11-04 16:54:43

标签: php yii

我想采用博客评论模型并将表单转换为多模式,但我无法解决这个问题。如果有人能指出我正确的方向,我将不胜感激。

采取下面的设计我想在评论链接表中添加另一个表(OtherModel)与评论。

enter image description here

控制器

public function actionView()
{
    $post=$this->loadModel();
    $comment=$this->newComment($post);

    $this->render('view',array(
        'model'=>$post,
        'comment'=>$comment,
    ));
}

protected function newComment($post)
{
    $comment=new Comment;
    $otherModel=new OtherModel;
    if(isset($_POST['Comment'], $_POST['OtherModel']))
    {
        $comment->attributes=$_POST['Comment'];
        $otherModel->attributes=$_POST['OtherModel'];
        if($post->addComment($comment))
        {
            if($comment->status==Comment::STATUS_PENDING)
                Yii::app()->user->setFlash('commentSubmitted','Thank you...');
            $this->refresh();
        }
    }
    return $comment;
}

模型

public function addComment($comment)
    {
        $comment->other_id=$otherModel->other_id;
        $otherModel->save();
        if(Yii::app()->params['commentNeedApproval'])
            $comment->status=Comment::STATUS_PENDING;
        else
            $comment->status=Comment::STATUS_APPROVED;
        $comment->post_id=$this->id;
        return $comment->save();
    }

通过CJuiTabs渲染表单

'Comment'=>$this->renderPartial('/comment/_form',array($model->$comment=>),true)

形式

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'comment-form',
        'enableAjaxValidation'=>true,
)); ?>

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

        <div class="row">
                <?php echo $form->labelEx($model,'author'); ?>
                <?php echo $form->textField($model,'author',array('size'=>60,'maxlength'=>128)); ?>
                <?php echo $form->error($model,'author'); ?>
        </div>

        <div class="row">
                <?php echo $form->labelEx($model,'content'); ?>
                <?php echo $form->textArea($model,'content',array('rows'=>6, 'cols'=>50)); ?>
                <?php echo $form->error($model,'content'); ?>
        </div>

        // added otherModel as part of MMF
        <div class="row">
                <?php echo $form->labelEx($otherModel,'name'); ?>
                <?php echo $form->textField($otherModel,'name',array('size'=>60,'maxlength'=>128)); ?>
                <?php echo $form->error($otherModel,'name'); ?>
        </div>

        <div class="row">
                <?php echo $form->labelEx($otherModel,'description'); ?>
                <?php echo $form->textArea($otherModel,'description',array('rows'=>6, 'cols'=>50)); ?>
                <?php echo $form->error($otherModel,'description'); ?>
        </div>

        <div class="row buttons">
                <?php echo CHtml::submitButton($model->isNewRecord ? 'Submit' : 'Save'); ?>
        </div>

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

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

2 个答案:

答案 0 :(得分:1)

我认为您在上面的代码中出现了错误:您尝试使用$otherModel变量代替$comment方法呈现的actionView()变量。

您是否尝试使用yiiframework.com上发布的示例?我的意思是你在评论中给出的链接。如果它不适合你,我担心我误解了你的问题。

答案 1 :(得分:0)

我认为你应该在保存之前验证这两个模型,否则你最终会在数据库中找到陈旧的$ otherModel记录。

protected function newComment($post)
{
    $comment=new Comment;
    $otherModel=new OtherModel;
    if(isset($_POST['Comment'], $_POST['OtherModel']))
    {
        $comment->attributes=$_POST['Comment'];
        $otherModel->attributes=$_POST['OtherModel'];
        // also specify $otherModel as a param
        if($post->addComment($comment, $otherModel))
        {
            if($comment->status==Comment::STATUS_PENDING)
                Yii::app()->user->setFlash('commentSubmitted','Thank you...');
            $this->refresh();
        }
    }
    return $comment;
}


public function addComment($comment, $otherModel)
{
    // Validate both models, return false if there are errors.
    // Errors should be available via $model->getErrors()
    if ($comment->validate() && $otherModel->validate()) {
        // save the otherModel first to obtain the generated ID
        $otherModel->save();
        $comment->other_id=$otherModel->id;                    
        if(Yii::app()->params['commentNeedApproval'])
            $comment->status=Comment::STATUS_PENDING;
        else
            $comment->status=Comment::STATUS_APPROVED;
        $comment->post_id=$this->id;
        return $comment->save();
    } else {
        return false;
    }
}

注意:您也可以在此处使用transactions