你好我们正在用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 -->
抱歉,我发不完整的代码太久了。
那么你能告诉我什么是错的吗?以及如何检查验证模型是否有错误?
修改
显示问题所在的地方!
答案 0 :(得分: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完成。对于此视图,您可以传递模型的实例。