【问题标题】:can't create a record in php yii framework无法在 php yii 框架中创建记录
【发布时间】:2012-04-04 20:10:13
【问题描述】:

大家好,我正在使用 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 -->

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

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

编辑

显示问题所在!

【问题讨论】:

    标签: php forms validation frameworks yii


    【解决方案1】:

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

    // 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 完成的。您可以将模型的实例传递给此视图。

    【讨论】:

    • 如果您需要进一步说明,请告诉我。使用我当前的解决方案,您应该能够看到数据库中的更改,但在您为其创建特定视图之前,您仍然无法看到应用程序中的更改。
    • @Irakli:将echo('There was some error!'); 部分替换为var_dump($model-&gt;getErrors()); 以查看验证错误。
    • 除了 DCoder 所说的。检查 post 变量,只需 var_dump($_POST); 以查看正在发送的 POST 数组实际上是 GamesForm,因为看到 $_POST['GamesForm'] 不为空。你也检查过数据库吗?值现在保存了吗?
    • ok, ["YII_CSRF_TOKEN"]= ... 是 $_POST 中的一个字段,然后 ["GamesValidator"]=> array(47) { ["title"]=> ...你的 $_POST 是一个数组,还有一个 ["GamesForm"] 数组吗?之后我会告诉你有关 getErrors 的信息
    • 表单 id 是游戏表单,但发送的数据在 "GamesValidator" 中,因此在您的 create() 中将 if(isset($_POST["GamesForm"])) 更改为 if(isset($_POST['GamesValidator']))。数组的名称由 echo $form-&gt;textField($model); 行自动从模型中获取,因此名称为 GamesValidator。同时更改$model-&gt;attributes=$_POST["GamesValidator"]
    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 2014-05-17
    相关资源
    最近更新 更多