sql-update - 如何在 symfony 5 中不使用表单创建更新功能
问题描述
我想在不使用表单的情况下更新示例联系人实体,我不知道这是我的代码:
/**
* @Route("/UpdateContact/{id}",name="editcontact")
*/
public function EditContact(Contact $Contact , Request $request ,ManagerRegistry $manager)
{
$contact = $this->getDoctrine()
->getRepository(Contact::class)
->findAll();
$request->get('responsable');
$request->get('telephone');
$request->get('email');
$request->get('note');
$contact->setResponsable($request->request->get('responsable'))
->setTelephone ($request->request->get('telephone'))
->setEmail ($request->request->get('email'))
->setNote ($request->request->get('note'));
$manager->persist($contact);
$manager->flush();
return $this->render('companyProfile.html.twig', [
// 'contactform'=>$contactform->createView(),
'Contact' => $contact,
]);
}
它不起作用,所以如果有人知道如何在没有表格的情况下更新,请帮助我,我现在被卡住了
解决方案
你需要EntityManagerInterface
而不是ManagerRegistry
所以你的代码需要是这样的:
<?php
public function EditContact(Contact $contact, Request $request, EntityManagerInterface $em)
{
$contact
->setResponsable($request->get('responsable'))
->setTelephone($request->get('telephone'))
->setEmail($request->get('email'))
->setNote($request->get('note'))
;
$em->persist($contact);
$em->flush();
// rest of your code
}
请参阅https://symfony.com/doc/current/doctrine.html#persisting-objects-to-the-database
推荐阅读
- c++ - “类不存在默认构造函数”即使我有构造函数?
- python - Python模块中全局变量中使用的补丁方法
- node.js - 如何在 node-api 和 angular 两端加密-解密有效负载/数据?
- python - 读取 json 文件 pandas 发生异常:ValueError arrays must be all be the same length
- android - FirestorePagingOptions 不会从 firestore 加载任何数据
- python - 无效的块标签错误。无法弄清楚我在哪里错过了结束块
- python - 我想建立一个 docker 环境,但是我得到了 ERROR: unsatisfiable constraints: python (missing):
- c# - 如何通过使用日期作为弹性搜索的字符串比较来获取文档?
- javascript - JS 从函数中获取未定义但服务器返回有效的 JSON
- android - 在 Xamarin Android Webview 中将 WebHistory 项目数组转换为字符串数组