简体   繁体   中英

Sonata admin bundle , Symfony2

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);
}

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.   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM