2015-04-06 29 views
0

我在我的模型中設置了一個屬性'!pagrawal_serial_number'作爲integer並在我的控制器我的代碼我正在使用一些條件爲此屬性賦值,但它沒有得到保存。Yii2:將數據保存到不安全的屬性?

即使我使用這個簡單的代碼,它不工作,這是沒有數據被保存在這個領域。

if ($model->load(Yii::$app->request->post()) && $model->save()) { 
     $model->pagrawal_serial_number = 39; 
     $model->save(); 
.... 

注:在我的另一個模型和控制器,我已經設置了類似的規則,這是工作的罰款。

我試圖做所有事情,但似乎沒有任何工作,我還能檢查什麼?

回答

0

好的我找到了解決方案的一部分。

我用這個代碼,以發現任何驗證錯誤:

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?

+0

也許你可以詳細說明一下。 onSave()可能會在模型的beforeSave()或afterSave()方面出現錯誤。如果您提供不完整的詳細信息,我們無法確定錯誤源自的行號。謝謝。 –

相關問題