大家好,我正在用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,
));
}
但它没有显示任何内容,网站再次显示表单.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 -->
对不起,我不能发布完整的代码,太长了。
那么你能告诉我出了什么问题吗?以及如何检查验证模型是否存在错误?
编辑
显示出问题所在的位置!
很简单,您的create()
方法中缺少$model->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完成的。您可以将模型的实例传递到此视图。