且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

防止多个人编辑同一表格

更新时间:2023-02-16 14:29:02

向您的 projects 数据库添加一个可为空的 locked_to 列.然后在 Project 模型中将此字段添加到 $ dates 数组中,以便可以将其自动转换为 Carbon 实例.

Add a nullable locked_to column to your projects database. Then in the Project model add this field to $dates array so it can be converted to Carbon instances automatically.

当有人打开项目进行编辑时,只需将 locked_to 字段设置为将来的日期,我认为+5秒可能是一个不错的选择.编辑表单应每5秒钟发送一次ajax请求,以使项目在接下来的5秒钟内保持锁定状态.

When someone opens the project for edit just set locked_to field to future date, i think +5 seconds may be a good choice. Edit form should send ajax request every 5 seconds to keep project locked for next 5 seconds.

当用户保存项目更改时,锁定将停止,因为将不发送任何ajax请求.在这种情况下,您还有一个字段可以告诉您上次打开项目的时间.

When user saves project changes, locking will be stopped cause no ajax requests will be send. In that case you also have field which may tell you when the project was opened for the last time.

如果 locked_to 字段等于或大于 new Carbon 实例,则用户将无法编辑项目.

User won't be able to edit project if locked_to field is equal or greater than new Carbon instance.

更新-更多信息

无需清除此字段.如果用户未完成编辑-他只是重新加载页面,导航到其他页面,关闭的窗口或浏览器,死亡等...用于锁定的Ajax将不再执行,因此在接下来的5秒钟内,当前编辑的项目将被解锁- locked_to 字段将包含比当前日期更早的日期.

There is no need to clear this field. If user didn't finish editing - he just reloaded page, navigated to other page, closed window or browser, died etc... ajax for locking won't be executed anymore, so in the next 5 second currently edited project will be unlocked - locked_to field will contain earlier date than current.