如何在 Zend Framework 上的数据库中保存复选框信息?

发布于 2024-11-08 18:00:07 字数 136 浏览 2 评论 0原文

我遇到以下问题:我正在创建一个表单来在应用程序上注册用户,并且在不同的输入类型中,有几个关于用户所说的语言的复选框。现在,由于用户可能会说不止一种语言,我想知道数据库中是如何处理的?我应该将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念﹏祤嫣 2024-11-15 18:00:07

我应该有 lang1, lang2,
lang3...等作为“用户”中的行
表?

这不是一个好主意。表结构应该是这样的:

user
-------------
| id | .... |

language
-------------
| id | name |

user_language
-------------------------
| user_id | language_id |

然后,例如,为了获取用户的语言选择,这个模拟查询将起作用:

SELECT language.name as language
FROM user
INNER JOIN user_language ON user_language.user_id = user.id
INNER JOIN language ON language.id = user_language.language_id
WHERE user.id = $id

这意味着您可以更新语言名称,并且名称更新将透明地发生,而无需担心编辑它在用户表的所有实例中。此方法还允许您为用户分配任意多种语言。

编辑

以语言为英语和西班牙语(id 1 和 id 2)为例,用户 ID 为 1。为了将用户与语言关联:

INSERT INTO user_language (user_id, language_id)
VALUES(1,1); <-- Associate user with English (id 1)
INSERT INTO user_language (user_id, language_id)
VALUES(1,2); <-- Associate user with Spanish (id 2)

现在,假设您使用 language 表中的值填充可用语言,该字段的值为 language.id,并且显示字段为语言.名称

Should I have lang1, lang2,
lang3...etc as rows in the 'users'
table?

That's not a good idea. The table structure should be something like this:

user
-------------
| id | .... |

language
-------------
| id | name |

user_language
-------------------------
| user_id | language_id |

Then to, for example, get the user's language choices, this mock query would work:

SELECT language.name as language
FROM user
INNER JOIN user_language ON user_language.user_id = user.id
INNER JOIN language ON language.id = user_language.language_id
WHERE user.id = $id

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:

INSERT INTO user_language (user_id, language_id)
VALUES(1,1); <-- Associate user with English (id 1)
INSERT INTO user_language (user_id, language_id)
VALUES(1,2); <-- Associate user with Spanish (id 2)

Now, this assumes that you're populating your available languages with the values in the language table, with the value of the field being language.id, and the display of the field being language.name.

七秒鱼° 2024-11-15 18:00:07

我自己是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文