actionCreate被多次调用 - Yii

时间:2014-07-23 09:10:12

标签: php yii

我遇到了一个错误,每当我提交一个gii生成的字段时,我的模型会多次保存。我已经创建了一个错误日志,以便我可以多次看到被调用的内容,并且我发现函数actionCreate是我的代码中被调用三次的部分(尽管有时是两次)。当我填写表单时,单击提交,错误日志显示actionCreate函数被调用三次。

The controller form looks like this 

/**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate()
    {
        $model=new Account;
        error_log("How many times do I call actionCreate");
        // To-Do make the user account creation update via ajax 
        // $this->performAjaxValidation($model);

        if(isset($_POST['Account']))
        {
            $model->attributes=$_POST['Account'];

            if($model->save())
            {
                echo 'do we reach here';
                $this->redirect(array('index','id'=>$model->id));
            }

        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }

我的表格也是这样的

<?php
/* @var $this AccountController */
/* @var $model Account */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'account-form',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
    'enableAjaxValidation'=>true,
)); ?>

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

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

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

    <div class="row">
        <?php echo $form->labelEx($model,'mobile_comp'); ?>
        <?php echo CHtml::dropDownList(CHtml::activeName($model,'mobile_comp'), $select, 
               $model->providerOptions, array('empty'=>'(Select Your Provider')); ?>
        <?php echo $form->error($model,'mobile_comp'); ?>
    </div>

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

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

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

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

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

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

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

2 个答案:

答案 0 :(得分:1)

错误在我的表单中,我有ajaxValidation =&gt; true而不是ajaxValidation =&gt; false 我偶然用一个字段多次调用ActionCreate函数。

答案 1 :(得分:1)

因为你在评论中排名:

// To-Do make the user account creation update via ajax 
// $this->performAjaxValidation($model);

如果通过Ajax请求,函数$this->performAjaxValidation($model);将验证表单数据,并将验证结果作为JSON返回,并提前停止应用程序执行。

在您的情况下,如果发生Ajax验证,则无需检查验证,问题是保存模型。

取消评论$this->performAjaxValidation($model);

  

另请注意:请添加功能代码$this->performAjaxValidation($model)