如何安全地使用UniqueEntity(在具有多个同时用户的站点上)

Jef*_*ens 8 php concurrency symfony doctrine-orm

聪明的人可以共享他们使用的设计模式,以避免Doctrine\Symfony中的这个基本和常见的并发问题吗?

场景:每个用户必须拥有唯一的用户名.

解决方案失败:

失败原因:在验证和保持用户之间,用户名可能会被用户使用.如果是这样,Doctrine在尝试保留最新用户时会抛出UniqueConstraintViolationException.

Jul*_*mur 2

这是我的以下答案的作用:

  • 如果发生约束冲突,它会优雅地向用户显示错误,就像验证器处理它一样,

  • 它可以防止不受“保护”的数据库更新破坏您的控制器逻辑(例如使用 UPDATE 语句或使用“不受保护”的控制器提交表单),

  • 它是一个独立于数据库的解决方案。

这是代码,并附有注释说明:

<?php

// ...

use Doctrine\DBAL\Exception\ConstraintViolationException;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper;

// ...

public function indexAction(Request $request)
{
    $task = new Task();

    $form = $this->createFormBuilder($task)
        ->add('name', TextType::class)
        ->add('save', SubmitType::class, array('label' => 'Create Task'))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $task = $form->getData();
        $em = $this->getDoctrine()->getManager();
        $em->persist($task);

        try {
            $em->flush();

            // Everything went well, do whatever you're supposed to.
            return $this->redirectToRoute('task_success');
        } catch (ConstraintViolationException $e) {
            // Reopen the entity manager so the validator can do jobs
            // that needs to be performed with the database (in example:
            // unique constraint checks)
            $em = $em->create($em->getConnection(), $em->getConfiguration());

            // Revalidate the form to see if the validator knows what
            // has thrown this constraint violation exception.
           $violations = $this->get('validator')->validate($form);

            if (empty($violations)) {
                // The validator didn't see anything wrong...
                // It can happens if you have a constraint on your table,
                // but didn't add a similar validation constraint.

                // Add an error at the root of the form.
                $form->add(new FormError('Unexpected error, please retry.'));
            } else {
                // Add errors to the form with the ViolationMapper.
                // The ViolationMapper will links error with its
                // corresponding field on the form.
                // So errors are not displayed at the root of the form,
                // just like if the form was validated natively.
                $violationMapper = new ViolationMapper();

                foreach ($violations as $violation) {
                    $violationMapper->mapViolation($violation, $form);
                }
            }
        }
    }

    return $this->render('default/new.html.twig', array(
        'form' => $form->createView(),
    ));
}
Run Code Online (Sandbox Code Playgroud)