Symfony 2:如何防止多个用户同时编辑同一个表单? [英] Symfony 2 : How to prevent multiple user editing the same form at the same time?

查看:23
本文介绍了Symfony 2:如何防止多个用户同时编辑同一个表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 SF 应用程序中,我有常规"表单(在控制器中使用 formType 生成).

On my SF application, i have "regular" form (generated with formType in controller).

假设 user1 编辑 formA,填写它需要几分钟时间.在此期间,user2 还编辑了 formA.你可以猜到,最后一个点击提交的用户获胜,另一个用户输掉了他填写的所有内容.

Let's say user1 edit the formA and it takes several minutes to fill it. In that period, user2 also edit the formA. You can guess, the last user that hit submit wins, and the other one looses all he filled.

我怎样才能防止这种情况发生?这不是多次提交(由同一用户)的情况.

How can i prevent this from happening? This is not the case of a multiple submit (by the same user).

我不知道这是否可能以及如何最好地实现它,但我的猜测是添加一些锁定"机制.

I don't know if it's possible and how to best implement it, but my guess would be to add some "lock" mechanism.

我已阅读https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/transactions-and-concurrency.html#locking-support

将 @Version 添加到我的表单的实体中的字段就足够了,SF 会负责其余的工作吗?

Would it suffice to add a @Version to a field in the entity for my Form and SF takes care of the rest ?

我希望当用户 2 尝试打开表单时,我可以发送消息:抱歉,用户 1 已经在完成表单".我不希望 user2 只有在点击提交时才会丢失他填写的所有内容.

I would like that when user2 tries to open the form, i can send the message : "Sorry, user1 is already completing the form". I don't want that user2 looses all he fills only when he hit submit.

谢谢.

推荐答案

框架没有提供开箱即用的功能.

What you are trying to achieve is not provided out of the box by the framework.

您可以做的是利用锁定组件或实施自己一个类似的解决方案.

What you can do is take advantage of the Lock Component or implement a similar solution by yourself.

// Symfony\Component\Lock\Factory $factory

$lock = $factory->createLock('your-amazing-form-USER-ID');

if (!$lock->acquire()) {
    // Sorry, user1 is already completing the form
    // throw exception or render a template for example
}

// Do what you need to show the form.

// Form is submitted and valid? Do what you need and release the lock.
if ($form->submited()) {
    // Save to database
    $lock->release();
}

一个好主意是给锁一个 TTL,这样如果用户没有提交表单,锁仍然会被释放.由于您仍然希望获得锁的用户能够提交表单,因此资源名称应包含例如用户标识符.

A good idea is to give a TTL to the lock so that if the user does not submits the form the lock is still released. Since you still want that the user who acquired the lock is able to submit the form, then the resource name should contain for example the user identifier.

这篇关于Symfony 2:如何防止多个用户同时编辑同一个表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆