Sonata管理包,Symfony2

时间:2017-01-19 05:33:38

标签: symfony doctrine-orm sonata-admin

我想在保存在sonata管理包中之前更改我的日期:

实际上尝试在preCreate方法中更改请求,但我什么也没得到......

这是我的代码(在我的自定义控制器上称为eventAdminController.php):

protected function preCreate(Request $request, $object)
{
    $jalali = $this->get('jalali');

    $all = $request->request->all();


    if(isset($request->request->get('s587f71334d196')['start_date'])){
        $start_date = explode('/',$request->request->get('s587f71334d196')['start_date']);

        $start_date_day = $start_date[0];
        $start_date_mon = $start_date[1];
        $start_date_yer = $start_date[2];

        $start_date = $jalali->to_miladi($start_date_yer,$start_date_mon,$start_date_day);

        $all['s587f71334d196']['start_date'] = $start_date[0].'-'.$start_date[1].'-'.$start_date[2];
    }


    if(isset($request->request->get('s587f71334d196')['end_date'])){
        $end_date = explode('/',$request->request->get('s587f71334d196')['end_date']);

        $end_date_day = $end_date[0];
        $end_date_mon = $end_date[1];
        $end_date_yer = $end_date[2];

        $end_date = $jalali->to_miladi($end_date_yer,$end_date_mon,$end_date_day);

        $all['s587f71334d196']['end_date'] = $end_date[0].'-'.$end_date[1].'-'.$end_date[2];
    }

    $request->request->replace($all);
}

1 个答案:

答案 0 :(得分:4)

您可以在管理员类中执行此操作,您需要为此添加prePersist和preUpdate方法。例如

public function prePersist($object)
{
    $this->changeDate($object);
}
public function preUpdate($object)
{
    $this->changeDate($object);
}

public function changeDate($object) 
{
  //Set your entity date.   
}