自动保存草稿的最佳实践?
对于在发送电子邮件之前自动保存电子邮件或在完成或正式保存之前自动保存博客文章的应用程序,最佳策略是什么? 最好在数据库中使用单独的表来存放临时草稿,还是使用一个状态列将帖子标记为草稿或已发布? 我不是在寻找代码,只是在寻找方法,但也欢迎任何其他相关建议,例如保存的频率等。
What is the best strategy for applications that autosave an email before it is sent or save a blog post before it's finished or officially saved? Would it be best to use a separate table in the database for temporary drafts or to have a status column that marks a post as draft or published? I'm not looking for code, just methods, but any other related advice would be welcome as well, like how often to save, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑到草稿和已发表文章的单独表格本质上是彼此重复的,我倾向于只用一个带有状态列的表格来区分两者。
Considering that separate tables for drafts and published articles would be essentially duplicates of each other, I would lean towards just one table with a status column to differentiate between the two.
我以维基百科的方式进行起草:我保存第一个版本,并将所有修改保存(基于时间或明确的用户命令)作为下一个版本。 即后。 您可以删除草稿图,也可以不删除。
如果将数据保存在数据库中,我认为最好使用同一个表(可以避免架构冲突),并使用版本/状态来跟踪草稿生命周期。
I do drafting on the Wikipedia way: I save the first version, and all modification's saved (based on time or explicit user command) as a next version. After ie. publication you can delete the draft-graph - or not.
If you save data in database I think it's good to use the same table (you can avoid schema conflicts), and use version/status to track drafts lifecycle.
这不仅仅适用于电子邮件……
我在这件事上改变了主意。 最好的方法是在表中使用 is_draft 列,并将草稿和有效实体存储在同一个表中。 这样做的优点是,即使实体切换进入和退出草稿状态,实体也会保持相同的 ID(您可能希望在保存后对其进行编辑,但暂时删除所需的值)。 如果用户在同一个文档上进行协作并且 ID 不断变化,那么用户会感到困惑,amirite?
您可以使用 is_draft=1 关闭 ORM 验证规则、触发验证或检查约束以允许保存无效对象。 是的,您可能必须允许表中存在可为空的字段。
过程:
尝试保存对象。 验证失败。 设置 is_draft=1 并再次尝试保存。 它节省了。 将大“草稿”放在屏幕上的某处:)
用户填写所需信息。 尝试保存对象。 验证通过。 设置 is_draft=0。 它节省了。
现在,关于电子邮件和博客文章,除非用户点击“保存/发布”按钮,否则您的服务器不应尝试立即发送或发布它,但这实际上是一个不同的问题。
旧答案
问题是草稿可能无效,并且无法保存在实际表中。 例如,假设您的表要求主题不为空,但用户尚未填写。
一种方法是拥有一个草稿表,并将实体(及其子实体)的序列化版本存储到其中。 php 的serialize() 可以使用,或者你可以使用json。 当它最终有效时,系统将保存到电子邮件(或其他)表,并删除草稿:
伪sql:
您还可以考虑使用草稿文件表来存储草稿的附件或照片,并能够访问它们单独:
因此,用户开始撰写电子邮件,可能只是输入正文,然后添加一些附件。 您的 gui 将电子邮件保存到草稿,上传附件,将它们保存到草稿文件,并返回草稿 id 以及您在 gui 中显示的文件的下载 url。
他输入主题(“To”仍然是空白)。 您的 GUI 将电子邮件保存到草稿中,并通过 ID 更新草稿表,因为它从上一步中知道了其 ID。
您的用户填写“收件人”字段,然后单击“发送”。 您的服务器将电子邮件保存到电子邮件表,将附件从草稿文件复制到电子邮件附件表,然后删除草稿(最好是在事务中)。
这允许长期草稿、gmail 式附件上传,同时保持真实实体表的完整性。
this applies to more than emails...
I changed my mind on this one. The best way is to use a is_draft column in your table and store both drafts and valid entities in the same table. this has the advantage of the entity keeping the same id even if it switches in and out of draft state (you might want to edit it after you save it, but temporarily remove a required value). it would be confusing for users if they were collaborating on the same document and the id kept changing, amirite?
you would use is_draft=1 to turn off ORM validation rules, trigger validations or check constraints to allow an invalid object to save. yes, you'd likely have to allow nullable fields in your table.
process:
try to save object. validation fails. set is_draft=1 and try to save again. it saves. put big "DRAFT" on the screen somewhere :)
user fills in required info. try to save object. validation passes. set is_draft=0. it saves.
now, regarding email and blog posts, your server shouldn't try to send it or post it right away unless the user hits the save/post button, but that is a different issue really.
OLD ANSWER
The problem is that a draft might not be valid, and cannot be saved in the actual table. For example, say your table demands that the subject be not null, but the user hasn't filled it in yet.
One way would be to have a draft table, and store a serialized version of the entity (and its children) to it. php's serialize() would be something to use, or you could use json. when it is finally valid, the system would save instead to the email (or whatever) table, and delete the draft:
pseudo sql:
you could also consider a draft_file table for storing attachments or photos for the draft, and be able to access them individually:
so, a user starts composing an email, maybe just types in the body, and adds some attachments. your gui saves the email to drafts, and uploads the attachments, saves them to draft_file, and returns the draft id, and the download urls for the files which you display in your gui.
he types in the Subject (To is still blank). Your gui saves the email to drafts, updating the draft table by id, as it knows its id from the previous step.
your users fills in the To field, and hits Send. Your server saves the email to the email table, copies the attachments from draft_file to the email_attachment table, and deletes the draft, preferably within a transaction.
this allows for long-term drafts, gmail-style attachment uploads, while maintaining integrity of your real entity table.