创建具有多个一对多关系的 DAO 查询?

发布于 2024-08-19 04:52:39 字数 918 浏览 1 评论 0原文

我正在使用 PHP 和 MySQL 开发一个 Web 应用程序。我有一个名为 User 的实体,其中有许多一对多关系(与其他对象)(地址列表、电子邮件列表、电话号码列表等)。这些许多地址、电子邮件、电话号码通过数据库中的联结表(user_link_addresses、user_link_emails 等)提供给用户。

我现在正在尝试为 User 实体创建一个高效的 DAO,特别是方法 readUserById($userId) 和 readAllUsers()。然而,由于我是 sql 和 DAO 模式的业余爱好者,因此我在正确创建这个 DAO 时遇到了困难。

有人告诉我,将 DAO 保持为一次读取所有信息然后可以相应地映射到该实体的一个查询对于网络效率非常重要。这确实有道理,但我无法生成一个查询,该查询包含所有内容的必要信息,并且可以正确映射到我的实体。

现在,我可以在获取用户后通过在循环内执行多个查询来获取此信息......我只是不喜欢这个想法(而且我确信它不正确)。另外,我知道如何与连接表进行连接 - 只是不使用如此复杂并且也需要正确映射的东西。

表名和字段

表:用户 字段:用户 ID、用户名、密码

表:地址 字段:地址 ID、街道、城市、州、邮政编码、类型

表:电子邮件 字段:email_id、电子邮件、类型

表:电话号码 字段:phone_number_id、area_code、phone_number、type

-- 关联的连接表 --

表:user_link_address 字段:user_id、address_id

表:user_link_email 字段:user_id、email_id

表:user_link_phone_number 字段:user_id、phone_number_id

无论如何,非常感谢所有帮助,

Steve

I am developing a web application with PHP and MySQL. I have an entity named, User, that has many one-to-many relationships (with other objects) within it (list of Addresses, list of Emails, list of PhoneNumbers, etc). These many addresses, emails, phone numbers are supplied to the user via junction tables in the database (user_link_addresses, user_link_emails, etc).

I am now trying to create an efficient DAO for the User entity, specifically methods readUserById($userId) and readAllUsers(). However, since I am an amateur at sql and the DAO pattern, I am having trouble creating this DAO correctly.

I have been told that it is important for network efficiency to keep your DAO to one query that reads all the information at once and then can be mapped to that entity accordingly. That does make sense, but I am having trouble generating one query that has the necessary information from everything and that can be mapped correctly to my entity.

Right now, I can get this information by performing multiple queries within a loop after getting the users... I just don't like that idea (and I'm sure it's not correct). Also, I know how to do joins with junction tables - just not with something this complex and that needs to be mapped correctly as well.

TABLE NAMES AND FIELDS

TABLE: user
FIELDS: user_id, username, password

TABLE: address
FIELDS: address_id, street, city, state, zip_code, type

TABLE: email
FIELDS: email_id, email, type

TABLE: phone_number
FIELDS: phone_number_id, area_code, phone_number, type

-- JUNCTION TABLES FOR ASSOCIATIONS --

TABLE: user_link_address
FIELDS: user_id, address_id

TABLE: user_link_email
FIELDS: user_id, email_id

TABLE: user_link_phone_number
FIELDS: user_id, phone_number_id

Anyway, all help is greatly appreciated,

Steve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

も让我眼熟你 2024-08-26 04:52:39

有时在查询中包含关联是值得的,有时则不然。 Ruby on Rails 有一个非常简洁的插件,可以在应用程序运行时对其进行分析,并告诉您哪些查询应包含关联的模型。

您可以尝试类似的操作:

SELECT *
    FROM `user` 
    INNER JOIN `user_link_addresses`
        ON `user`.id = `user_link_addresses`.user_id
    INNER JOIN `user_link_foo`
        ON `user`.id = `user_link_foo`.user_id

如果您告诉我所有表名和字段,我可以帮助您编写更具体的查询。

Sometimes it's worth it to include associations within queries and sometimes it's not. There's a really neat plugin for Ruby on Rails that analyzes your application as it runs and tells you which queries should include associated models.

You could try something like:

SELECT *
    FROM `user` 
    INNER JOIN `user_link_addresses`
        ON `user`.id = `user_link_addresses`.user_id
    INNER JOIN `user_link_foo`
        ON `user`.id = `user_link_foo`.user_id

If you tell me all your table names and fields, I could help you write a more specific query.

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