gpt4 book ai didi

php - Yii 模型规则 "Failed to set unsafe attribute"

转载 作者:行者123 更新时间:2023-12-05 03:15:00 25 4
gpt4 key购买 nike

我一直在寻找,但无法让它发挥作用。

这些是我在模型 Acta.php 中的规则

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

这是 Controller 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
}

另一方面,您似乎在特定情况下将某些属性设置为 safe。但是你没有设置场景

要设置场景,请执行以下操作:

$model->setScenario('THE SCENARIO NAME');

或者:

$model=new YOURMODELNAME('SCENARIO NAME');

希望对你有帮助

关于php - Yii 模型规则 "Failed to set unsafe attribute",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21587072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com