Php “Yii示范规则”;未能设置不安全属性";
我一直在找,但没法用 这些是我在Acta.php模型中的规则Php “Yii示范规则”;未能设置不安全属性";,php,yii,Php,Yii,我一直在找,但没法用 这些是我在Acta.php模型中的规则 public function rules() { return array( array('dominio', 'required'), array('numero, velocidad, grupo, dni, cuit, plan_pago_id', 'numerical', 'integerOnly'=>true), array('foto, observacion
public function rules()
{
return array(
array('dominio', 'required'),
array('numero, velocidad, grupo, dni, cuit, plan_pago_id', 'numerical', 'integerOnly'=>true),
array('foto, observaciones, situacion, infractor, dominio, tipo_vehiculo, marca, modelo, domicilio, codigo_postal, localidad, provincia, tipo_multa, usuario', 'length', 'max'=>255),
array('hora', 'length', 'max'=>11),
//Here is the problem with only this three attributes
array('municipio, cinemometro, fecha_labrada', 'safe', 'on'=> 'create,update'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, numero, fecha_labrada, velocidad, grupo, foto, observaciones, situacion, infractor, dominio, dni, cuit, tipo_vehiculo, marca, modelo, domicilio, codigo_postal, localidad, provincia, tipo_multa, hora, usuario, plan_pago_id', 'safe', 'on'=>'search'),
);
}
这是控制器ActaController.php上的代码
public function actionCreate()
{
$model = new Acta;
if(isset($_POST['Acta']))
{
...
code setting data on $_POST['Acta']
...
$model->attributes = $_POST['Acta'];
$model->save();
}
$this->redirect(array('ingresar'));
}
我看不出有什么问题。应该正常工作吗
编辑:我以为场景是自动设置的。我错了。
要解决此问题,必须在属性之前设置场景:
...
$model->setScenario('create');
$model->attributes = $_POST['Acta'];
...
在保存之前,您肯定有一些错误。要了解错误,请执行以下操作:
if($model->validate()){
//NO ERRORS, SO WE PERFORM SAVE PROCESS
$model->save()
}else{
//TO SEE WHAT ERROR YOU HAVE
CVarDumper::dump($model->getErrors(),56789,true);
Yii::app()->end();
//an alternative way is to show attribute errors in view
}
$model->setScenario('THE SCENARIO NAME');
另一方面,您似乎在特定场景中将某些属性设置为safe
。但是您没有设置场景
要设置场景,请执行以下操作:
if($model->validate()){
//NO ERRORS, SO WE PERFORM SAVE PROCESS
$model->save()
}else{
//TO SEE WHAT ERROR YOU HAVE
CVarDumper::dump($model->getErrors(),56789,true);
Yii::app()->end();
//an alternative way is to show attribute errors in view
}
$model->setScenario('THE SCENARIO NAME');
或:
我希望这会有所帮助您是否设置了所有分配给“安全”的属性?@themosquitokiller您的意思是什么?比如测试
数组(“…所有属性…”,“'safe'),
?谢谢。我确实尝试过设置场景,但我在错误的地方(在设置属性之后)进行了设置。现在,我将按照您的建议,在保存之前使用验证。场景!这个答案让我找到了解决类似的不安全属性错误的正确途径。