【发布时间】:2017-09-26 06:44:01
【问题描述】:
正如标题所说,我需要在表单事件中获取要删除的实体。我正在使用 Symfony 2.7。如果它被创建/编辑,我可以在POST_SUBMIT 事件中获取实体,但在删除之前我无法在PRE_SUBMIT、SUBMIT 和POST_SUBMIT 上获取它。
到目前为止我尝试了什么(我在 cmets 中写了变量的结果)
public static function getSubscribedEvents()
{
return array(
FormEvents::POST_SUBMIT => 'onPostSubmit',
FormEvents::PRE_SUBMIT => 'onPreSubmit',
FormEvents::SUBMIT => 'onSubmit'
);
}
public function onPreSubmit(FormEvent $event)
{
dump($event->getForm()->getData()); // <-- null
dump($event->getData()); // <-- array:1 ["submit" => ""]
}
public function onSubmit(FormEvent $event)
{
dump($event->getForm()->getData()); // <-- null
dump($event->getData()); // <-- array:0 []
}
public function onPostSubmit(FormEvent $event)
{
dump($event->getForm()->getData()); // <-- array:0 []
dump($event->getData()); // <-- array:0 []
}
这基本上是删除的开始。我不会把它使用的整个函数都放上去,因为我认为不需要:
public function deleteConfirmAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);
$entity = $coreService->getArea($id);
if ($form->isValid()) {
$coreService->deleteEntity($entity); // will remove also relationships
$coreService->persistChanges(); // basically a Doctrine flush()
$this->addFlash('success', GlobalHelper::get()->getCore()->translate('flash.entity.delete.success'));
return $this->redirect($this->generateUrl('index')));
}
}
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('area_delete_confirm', array('id' => $id)))
->setMethod('POST')
->add('submit', 'submit', array('label' => 'entity.delete', 'attr' => ['class' => 'btn button-primary-right']))
->getForm();
}
有什么想法吗?
【问题讨论】:
-
你能显示代码如何删除你的实体吗?
-
当然@gintko,我会更新问题。
-
和你的
$this->createDeleteForm($id)方法? -
没什么特别的@gintko,但我还是添加了它:)