玩!框架 CRUD 模块:添加默认值并更改日期格式?
我正在使用“玩”!框架 CRUD 模块,但我无法弄清楚:我的数据库表有一个 created
字段,它基本上是创建行的时间。我不想让我的用户设置这个,我想在后端完成它,只需添加当前时间。但我不知道该怎么做。
我已经使用 @Hidden 使该字段不可见,但显然现在我无法创建新行,因为它的值根本没有设置。那么我该在哪里执行此操作呢?
我还有另一个问题:我的表还有一个名为 publish
的列,它是另一个时间戳。 CRUD 表单中该字段的当前格式为 yyyy-MM-dd。我也想指定一个日期,但不知道如何......
有人可以帮忙吗?
I'm using Play! frameworks CRUD module but I can't figure something out: my database table has a created
field which is basically the time that a row was created. I don't want my user to set this, I want to do it in the backend, simply add the current time. I can't figure out how to do this though.
I have made the field invisible using @Hidden
but obviously now I can't create new rows because it's value simply isn't set. So where do I do this?
And another question I have: my table also has a column called publish
which is another timestamp. The current format for this field in the CRUD form is yyyy-MM-dd. I would like the specify a date as well, and can't figure out how..
Can someone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 javax.persistence.PrePersist 注释来设置创建日期。将此方法放入您的模型中:
You could use the javax.persistence.PrePersist annotation to set the created date. Put this method in your model:
您可以在 CRUD 模板中使用自定义字段渲染来显示格式化的值或使用您想要的任何控件(即:用于日期的 jquery 日期选择器)。
要隐藏值并分配默认值,首先通过删除字段从 CRUD 的编辑/空白表单中删除该值。然后重写实体中的 _save() 方法(注意开头的 _,您需要 _save(),而不是 save()),并在调用 super._save() 之前在代码中设置所需的值。像这样:
you can use custom field rendering in CRUD templates to display the values formatted or using any control you want (i.e.: a jquery date picker for dates).
To hide a value and assign a default value, first of all remove the value from the edit/blank forms of CRUD by removing the field. Then override the _save() method from the entity (be careful with the initial _, you want the _save(), not save()) and set in the code the values you want before calling super._save(). Like this: