我有一个表,其中的列for_date
按数据库中的整数类型保存。为了以for_date
DateTime 格式显示,我使用了带有代码的 ActiveForm:
<?= $form->field($model, 'for_date')->textInput([
'class' => 'form-control datepicker',
'value' => $model->for_date ? date(Yii::$app->params['dateFormat'], $model->for_date) : date(Yii::$app->params['dateFormat'], time()),
'placeholder' => 'Time'])
->label('Attendance Date') ?>
但是当我创建和保存时,Yii 通知了This field must be integer
在模型文件中,我有以下 2 个函数在验证之前进行转换,但它仍然是错误的。
public function beforeValidate(){
if(!is_int($this->for_date)){
$this->for_date = strtotime($this->for_date);
$this->for_date = date('d/M/Y', $this->for_date);
}
return parent::beforeValidate();
}
public function afterFind(){
$this->for_date = \Yii::t('app', Yii::$app->formatter->asDate($this->for_date));
$this->for_date = date('d/M/Y', $this->for_date);
return parent::afterFind();
}
我怎样才能正确地用整数保存到数据库中?