[英]Sonata admin bundle , Symfony2
I want to change my date before to save in sonata admin bundle : 我想更改日期以保存在Sonata管理员捆绑包中:
in fact try to change request in preCreate
Method but I got nothing ... 实际上尝试更改
preCreate
方法中的请求,但我什么也没有...
here is my code (on my custom controller called eventAdminController.php
): 这是我的代码(在名为
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. 您可以在管理类中执行此操作,为此需要添加prePersist和preUpdate方法。 For example
例如
public function prePersist($object)
{
$this->changeDate($object);
}
public function preUpdate($object)
{
$this->changeDate($object);
}
public function changeDate($object)
{
//Set your entity date.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.