Sonata admin bundle , Symfony2

2019-07-24 08:31发布

I want to change my date before to save in sonata admin bundle :

in fact try to change request in preCreate Method but I got nothing ...

here is my code (on my custom controller called 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条回答
Animai°情兽
2楼-- · 2019-07-24 09:05

You can do this in your Admin Class you need to add prePersist and preUpdate method for that. For example

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

public function changeDate($object) 
{
  //Set your entity date.   
}
查看更多
登录 后发表回答