1

大家好,我正在使用 php 和 Yii 框架创建一个网站。现在我已经创建了一个管理模块并在这个模块中创建了 crud,但似乎我无法创建记录。我现在得到的是:

    public function actionCreate()
    {
        $model=new GamesValidator;

        // Uncomment the following line if AJAX validation is needed
        $this->performAjaxValidation($model);
        /*
        if(isset($_POST['ajax']) && $_POST['ajax']==='games-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        */
        if(isset($_POST['GamesForm']))
        {
            die('GamesForm is set'); // just to see if GamesForm has some value! but website never shows this massege, it shows just new form, which is my problem.
/*
            $model->attributes=$_POST['GamesForm'];
            if($model->validate()){
                echo ('This is only a test');
                return;
            } else {
                echo ('There was some error!');
                return;
            }
*/
        }

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

但它没有显示任何内容,网站再次显示 form.php,就像什么都没做一样。这是我的视图文件中的一些代码:

<div class="form">

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

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

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

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

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

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

抱歉,我无法发布完整的代码,它太长了。

所以你能告诉我有什么问题吗?以及如何检查验证模型是否有错误?

编辑

显示出问题的地方!

4

1 回答 1

3

$model->save()很简单,您的方法中缺少create():-

// you'll have to remove the die() of course, otherwise the rest of the code won't be executed
if($model->validate()){
        echo ('This is only a test');
        // the next line is important to save records, we are passing false because validation is already done
        $model->save(false);
        return;
    } else {
        echo ('There was some error!');
        return;
    }

阅读更多关于CActiveRecord中的方法。

编辑

要查看应用程序中的更改,您需要为新创建的记录创建一个视图。在 yii 自动生成的代码(使用 gii)中,它是通过 CDetailView 完成的。您可以将模型的实例传递给此视图。

于 2012-04-04T21:07:02.373 回答