Yii2:将数据保存到不安全的属性?
Yii2: Saving data to unsafe attribute?
我在我的模型中将一个属性设置为 '!pagrawal_serial_number'
作为 integer
并且在我的控制器中我的代码中我正在使用一些条件为该属性分配一个值,但它没有得到已保存。
即使我使用这个简单的代码,它也不起作用,因为该字段中没有保存数据。
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->pagrawal_serial_number = 39;
$model->save();
....
注意:在我的另一个模型和控制器中,我设置了类似的规则,工作正常。
我尝试了所有操作,但似乎无济于事,我还能检查什么?
好的,我找到了部分解决方案。
我使用此代码查找任何验证错误:
if ($model->validate()) {
$model->pagrawal_serial_number = 39;
} else {
$errors = $model->errors;
var_dump($errors);exit;
}
这给了我一个错误 appointment_date is not formatted correctly
我在我的模型中为预约日期设置了这样的规则:
[['appointment_date'], 'date','format' => 'php:d-M-Y H:i a'],
我注释掉了这条 appointment_date
规则,现在我可以将数据保存到问题中提到的不安全属性。
Only thing I wonder is how I am able to save the same in the first
instance and the validation fails on the second instance?
我在我的模型中将一个属性设置为 '!pagrawal_serial_number'
作为 integer
并且在我的控制器中我的代码中我正在使用一些条件为该属性分配一个值,但它没有得到已保存。
即使我使用这个简单的代码,它也不起作用,因为该字段中没有保存数据。
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->pagrawal_serial_number = 39;
$model->save();
....
注意:在我的另一个模型和控制器中,我设置了类似的规则,工作正常。
我尝试了所有操作,但似乎无济于事,我还能检查什么?
好的,我找到了部分解决方案。
我使用此代码查找任何验证错误:
if ($model->validate()) {
$model->pagrawal_serial_number = 39;
} else {
$errors = $model->errors;
var_dump($errors);exit;
}
这给了我一个错误 appointment_date is not formatted correctly
我在我的模型中为预约日期设置了这样的规则:
[['appointment_date'], 'date','format' => 'php:d-M-Y H:i a'],
我注释掉了这条 appointment_date
规则,现在我可以将数据保存到问题中提到的不安全属性。
Only thing I wonder is how I am able to save the same in the first instance and the validation fails on the second instance?