我有一个这样的数组:
$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);
value成功保存,但没有"false"数据未保存
当看到验证错误大于error时:
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;
}
您应该更改beforeValidate的日期格式,或者更改holiday_date
属性的验证规则,因为beforeSave在验证通过后执行。
public function beforeValidate()
{
if ($this->isNewRecord)
{
$this->holiday_date = date('Y-m-d', strtotime($this->holiday_date));
}
return parent::beforeValidate();
}