[英]In Symfony3 I am trying to persist form data to the database, but it doesn't go to the current user's row, instead creates a new entry
[英]Symfony 3.1: Editing form creates new database entry instead of updating
我在 symfony 中更新包含兩個實體(“事件”和“信息”)的表單時遇到問題。一切正常,直到用戶嘗試更新信息。當他們這樣做時,所有內容都保存為新的數據庫條目。
public function editAction(Request $request, Event $event)
{
$user = $this->getUser();
$editForm = $this->createFormBuilder()
->setMethod('PUT');
$editForm->add("event", 'AppBundle\Form\EventType');
$editForm->add("info", 'AppBundle\Form\InfoType');
$editForm = $editForm->getForm();
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$formEvent = $editForm->get('event')->getData();
echo "Debug<br />";
echo "form id: ". $formEvent->getId(); // Wrong. Returns null.
echo "<br /> event id: ". $event->getId(); // The correct id of entity that should be updated.
$em->merge($formEvent);
$em->flush();
} else {
$editForm->get('event')->setData($event);
}
return $this->render('event/edit.html.twig', array(
'event' => $event,
'edit_form' => $editForm->createView()
));
我回顯了一些調試信息,並且 handleRequest() 確實將新信息映射到 $editForm->get('event') 中找到的事件實體中,但它的 Id 丟失了,我認為這與創建有關的新實體而不是更新。 怎么了?
路由: event_edit: path: /{id}/edit defaults: { _controller: "AppBundle:Event:edit" } methods: [GET, POST, PUT]
樹枝:
{{ form_start(edit_form, {'attr': {'novalidate': 'novalidate'}}) }}
{{ form_errors(edit_form) }}
{{ form_widget(edit_form.event.title) }}
{{ form_widget(edit_form.info) }}
<input type="submit" value="Save" />
{{ form_end(edit_form) }}
如果事件實體是您要更新的實體並且它由學說管理(已加載),則您需要將事件實體傳遞給您的表單。 試試這個:
public function editAction(Request $request, Event $event)
{
$user = $this->getUser();
$editForm = $this->createFormBuilder($event)
->setMethod('PUT');
$editForm->add("event", 'AppBundle\Form\EventType');
$editForm->add("info", 'AppBundle\Form\InfoType');
$editForm = $editForm->getForm();
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($event);
$em->flush();
}
return $this->render('event/edit.html.twig', array(
'event' => $event,
'edit_form' => $editForm->createView()
));
}
因此,由於我在更新實體方面也遇到問題 - 我會為其他人留下信息。
Symfony 版本: 4.3.x
$contact_form = $this->app->forms->contact()->handleRequest($request);
此代碼只是返回表單然后傳遞請求
public function contact(array $params = [], MyContact $my_contact_type = null): FormInterface {
return $this->createForm(MyContactType::class, $my_contact_type, $params);
}
這不會更新現有表單。 您必須將實體作為數據(第二個參數)傳遞。
現在還有另一件有趣的事情,我是通過艱難的方式學到的。
我呈現的視圖使用來自其他表單的contact_form和字段,因此我有一種機制可以從請求中提取子表單並提交它們。 最后我得到了帶有原始表單數據的Request
。
所以現在如果你做$form->getData
,你會從請求中獲得整個實體構建。 我已經修改了我的視圖,以便我也填寫 id 表單,因此我有 100% 相同的實體已經存在於 DB - 1:1 中進行了比較。
保留根據請求構建的此類實體將導致創建新實體。 您必須從數據庫中獲取實體才能更新它。
所以這就是我處理這種情況的方式:
public function saveEntity(MyContact $my_contact, bool $search_and_rebuild_entity = false){
if( $search_and_rebuild_entity ){
$id = $my_contact->getId();
$name = $my_contact->getName();
$contacts = $my_contact->getContacts();
$description = $my_contact->getDescription();
$image_path = $my_contact->getImagePath();
$name_background_color = $my_contact->getNameBackgroundColor();
$description_background_color = $my_contact->getDescriptionBackgroundColor();
$found_entity = $this->find($id);
if( !empty($found_entity) ){
$my_contact = $found_entity;
$my_contact->setName($name);
$my_contact->setContacts($contacts->toJson());
$my_contact->setImagePath($image_path);
$my_contact->setDescription($description);
$my_contact->setNameBackgroundColor($name_background_color);
$my_contact->setDescriptionBackgroundColor($description_background_color);
}
}
$this->_em->persist($my_contact);
$this->_em->flush();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.