简体   繁体   中英

can't create a record in php yii framework

Hello guys I am creating a website with php and Yii framework. Right now I have created a admin module and created crud in this module, but it seems that I can't create a record. what I have got for now is:

    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,
        ));
    }

but it doesn't show anything, website shows the form.php again like nothing was done at all. here is a little code from my view file:

<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 -->

sorry I can't post a full code it is too long.

so can you tell me what can be wrong? and how to check if validation model has some errors?

EDIT

showed the place where is the problem!

Simple, you are missing $model->save() in your create() method:-

// 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;
    }

Read more about the methods in CActiveRecord .

Edit :

To see the changes in your app, you will need to make a view for the newly created record. In yii auto generated code (with gii) it is done through CDetailView. To this view you can pass the model's instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM