2017-01-19 111 views
1

我想改變我的日期前奏鳴曲管理包保存:索納塔管理包,Symfony2的

其實嘗試在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); 
} 

回答

4

您可以在您需要添加prePersist和更新前的方法,你的管理類做到這一點。例如

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

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