YII:在 CRUD 之前转换数据格式


YII: Converting data format before CRUD?

我有一个输入,接受这种格式的 mm/dd/yyyy 的数据。

public function rules()
{
    return array(
        array('purchase_date', 'type', 'type' => 'date', 'message' => '{attribute}: is not a date, try this formate -> (mm/dd/yyyy) !', 'dateFormat' => 'MM/dd/yyyy'),
);
}

但是,问题是,从输入中捕获格式后,如何将格式更改为 YYYY-MM-DD为了以这种格式 YYYY-MM-DD 将其放入数据库中。

例。。。用户输入的日期以月/日/年为单位在保存模型之前,我以某种方式将其转换为 YYYY-MM-DD。

在模型中使用beforeSave函数并添加逻辑来转换日期格式

public function beforeSave(){
    $this->purchase_date = date("Y-m-d", strtotime($this->purchase_date));
    return true;
}