【问题标题】:how can i create an update function with out using form in symfony 5如何在 symfony 5 中不使用表单创建更新功能
【发布时间】:2021-08-16 12:49:51
【问题描述】:

我想在不使用表单的情况下更新示例联系人实体 我不知道怎么做 这是我的代码:

/** 
 * @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,
     ]);
  }

它不起作用,所以如果有人知道如何在没有表格的情况下更新,请帮助我,我现在卡住了

【问题讨论】:

    标签: sql-update edit symfony-forms symfony5


    【解决方案1】:

    你需要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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      相关资源
      最近更新 更多