如何在 Zend Framework 上的数据库中保存复选框信息?
我遇到以下问题:我正在创建一个表单来在应用程序上注册用户,并且在不同的输入类型中,有几个关于用户所说的语言的复选框。现在,由于用户可能会说不止一种语言,我想知道数据库中是如何处理的?我应该将 lang1、lang2、lang3...等作为“用户”表中的行吗?
I have the following issue: I'm creating a form to register users on an application and within the different input types there are a couple of checkboxes regarding the language the user speaks. Now since the user might speak more than just one language I was wondering how is that handled in the db? Should I have lang1, lang2, lang3...etc as rows in the 'users' table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是一个好主意。表结构应该是这样的:
然后,例如,为了获取用户的语言选择,这个模拟查询将起作用:
这意味着您可以更新语言名称,并且名称更新将透明地发生,而无需担心编辑它在用户表的所有实例中。此方法还允许您为用户分配任意多种语言。
编辑:
以语言为英语和西班牙语(id 1 和 id 2)为例,用户 ID 为 1。为了将用户与语言关联:
现在,假设您使用
language
表中的值填充可用语言,该字段的值为language.id
,并且显示字段为语言.名称
。That's not a good idea. The table structure should be something like this:
Then to, for example, get the user's language choices, this mock query would work:
This means you can update the language names and the name update will happen transparently without you having to worry about editing it in all instances of the user table. This method also allows you to assign as many languages as you want for the user.
EDIT:
Take for example if language is English and Spanish (id 1 and id 2), and the User's ID is 1. In order to associate the user with the languages:
Now, this assumes that you're populating your available languages with the values in the
language
table, with the value of the field beinglanguage.id
, and the display of the field beinglanguage.name
.我自己是 Zend 的新手(我是 BSC 的四年级学生),但这就是我要做的。
我会创建一个相关表,例如在用户表中具有多行的spokesLanguages,在我看来,大多数行都为空从来都不是一个好主意,在保存用户时,将应用程序重定向到speaksLanguages控制器createAction()并保存用户语言那里。
您将需要创建一个会话来在重定向中保留新用户。
我再次对此感到陌生,但我已经使用这种逻辑做了类似的事情。
I'm a complete newbie in Zend myself (I'm a 4th year BSC student) but this is what I would do.
I would create a related table such as speaksLanguages having multiple rows in the users table that most will be null is never a good idea in my opinion, on saving the user have your application redirect to the speaksLanguages controller createAction() and save the users languages there.
You will need to create a session to persist the new user across the redirect.
Again I'm new to this but I've done something similar using this logic.