我有一个像这样的数组:
$holiday = array(array('h_date' => $new_year, 'd_name' => 'New Year'),
array('h_date' => $memorial_day, 'd_name' => 'Memorial Day')
foreach($holiday as $holidays){
$date = $holidays["h_date"];
$name = $holidays["d_name"]
当我保存在mysql数据库中时
$model = new Holiday();
$model->holiday_date = $date;
$model->display_name = $name;
$model->save();
我写的时候
$model->save(false);
值已成功保存但未保存“false”数据
当看到验证错误而不是错误时:
Array ( [holiday_date] => Array ( [0] => The format of Holiday Date is invalid.))
我们用左
protected function beforeSave(){
if(parent::beforeSave()){
$this->holiday_date=date('Y-m-d',strtotime($this->holiday_date));
return TRUE;
}
return false;
}
答案 0 :(得分:1)
您应该更改beforeValidate上的日期格式,或更改holiday_date
属性的验证规则,因为在验证通过后执行beforeSave。
public function beforeValidate()
{
if ($this->isNewRecord)
{
$this->holiday_date = date('Y-m-d', strtotime($this->holiday_date));
}
return parent::beforeValidate();
}