如何在Symfony 4中创建“更新”表单?

时间:2018-08-08 19:14:39

标签: forms symfony sql-update formbuilder

我要使用此表单更新数据库条目:

myController.php

  public function index($id, $slug, Request $request, UserPasswordEncoderInterface $passwordEncoder)
  {

      $item = new User();

      $item= $this->getDoctrine()->getRepository(User::class)->find($id);
      $form = $this->createFormBuilder($item)
      ->add('username', TextType::class, array('attr' => array('class' => 'form-control')))
      ->add('email', EmailType::class, array('attr' => array('class' => 'form-control')))
      ->add('is_active', HiddenType::class)
      ->add('plainPassword', RepeatedType::class, array('type' => PasswordType::class,'invalid_message' => 'The password fields must match.','options' => array('attr' => array('class' => 'password-field')),'required' => true,'first_options'  => array('label' => 'Passwort', 'attr' => array('class' => 'form-control')),'second_options' => array('label' => 'Passwort wiederholen', 'attr' => array('class' => 'form-control')),))
      ->add('cancel', ButtonType::class, array('label' => 'Cancel','attr' => array('class' => 'cancel form-btn btn btn-default pull-right close_sidebar close_h')))
      ->add('update', SubmitType::class, array('label' => 'Update','attr' => array('class' => 'form-btn btn btn-info pull-right','style' => 'margin-right:5px')))
      ->getForm();
      $form->handleRequest($request);

     if($form->isSubmitted() && $form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->flush();
            return $this->redirectToRoute('pages', array(
              'slug' => $slug,
            ));
      }
      return $this->render('form.html.twig', ['form' => $form->createView()]);

}

form.html.twig:

{{ form_start(form) }}
<div>
  {{ form_label(form.username) }}
  {{ form_widget(form.username) }}
  {{ form_errors(form.username) }}
  {{ form_help(form.username) }}
</div>
<div>
  {{ form_label(form.email) }}
  {{ form_widget(form.email) }}
  {{ form_errors(form.email) }}
  {{ form_help(form.email) }}
</div>

<div>
  {{ form_widget(form.is_active) }}
  {{ form_errors(form.is_active) }}
  {{ form_help(form.is_active) }}
</div>

<div>
  {{ form_label(form.plainPassword.first) }}
  {{ form_widget(form.plainPassword.first) }}
  <div class="text-red">{{ form_errors(form.plainPassword.first) }}</div>
  {{ form_help(form.plainPassword.first) }}
</div>
<div>
  {{ form_label(form.plainPassword.second) }}
  {{ form_widget(form.plainPassword.second) }}
  <div class="text-red">{{ form_errors(form.plainPassword.second) }}</div>
  {{ form_help(form.plainPassword.second) }}
</div>

{{ form_end(form) }}

但是当我编辑表单中的用户名并单击“更新”时,我会收到错误消息

  

执行'INSERT INTO成员(用户名,   密码,电子邮件,is_active)带有参数[“ cat2”的VALUES(?,?,?,?)'   “ 123”,“ cat@cat.de”,“ 1”]:

     

SQLSTATE [23000]:违反完整性约束:1062重复的条目   密钥'UNIQ_12A0D2FFE7927C74'的'cat@cat.de'

我实际上希望SQL进行更新而不是INSERT ...

1 个答案:

答案 0 :(得分:0)

您需要从UserRepository :: find($ id)加载用户

use App\Repository\UserRepository;
// ...

class MyController
{    
    public function index($id, $slug, 
                          Request $request,
                          UserPasswordEncoderInterface $passwordEncoder,
                          UserRepository $userRepository)
    {

        $item = $userRepository->find($id);

        if(NULL === $item) {
            throw $this->createNotFoundException('could not find this user.');
        }

        $form = $this->createFormBuilder($item)

        // ...
    }

    // ...
}