php - dao模式和关系

标签 php design-patterns dao

我在 PHP 中使用 DAO 模式。我了解以这种方式分离模型的好处,但我不明白的是,当您的表通过关联实体相关时,您应该如何构建 DAO 和 VO

我举个例子:

在我的数据库中有

USERS(id,username);

USERS_POSTS(id_user(FK),id_post(FK));

POSTS(id, title);

USER_COMMENTS(id_user(Fk),id_post(FK));

COMMENTS(id, text);

我创建 UserVOPostVO 以及相应的 setter 和 getter,然后 UserDAOPostDAO 负责 SQL最后返回 VOs 。 对来自这些表的数据执行 CRUD 操作非常简单,但是当您开始考虑关联表并检索跨不同表的数据时,您就会开始认为使用 DAO 不再那么简单了......

如果您想返回文章作者所做的所有评论,您将如何组织您的 DAO 模式?我不需要 SQL 查询,我只是将其作为真实情况的示例...

我读到,为每个关联表配备关联 DAO 和 Vo 是个好主意。 它的 VO 包括什么?只有 2 个外键或来自两个表的所有属性?

如果逻辑为关联实体使用 DAO 和 VO,如果查询“通过”超过 3 个表(使用 2 个关联实体),解决方案是什么?

我怀疑 DAO 模式会有名为 users_posts_comments_article 的对象 :)))

谢谢

最佳答案

作为您自己,您想要获取什么样的数据并编写一个提供该数据的层。不要考虑将连接两个以上表的类称为什么。 您正在考虑将您的表格变成模型,并且您可能正朝着不适合您的项目的方向前进。由于我不知道你的项目有多大,所以我不能说这是否可行。

这里的读物肯定会给您一些启发:http://weierophinney.net/matthew/archives/202-Model-Infrastructure.html

引用那篇文章(他指的是领域模型):

When you think in these terms, you start breaking your system into discrete pieces that you need to manipulate, as well as consider how each piece relates to the others. This type of exercise also helps you stop thinking of your model in terms of database tables; instead, your database becomes the container in which data is persisted from one use of your model to the next. Your model instead is an object that can do things with either incoming or stored data -- or even completely autonomously.

关于php - dao模式和关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6467568/

相关文章:

php - 更新上传文件夹中的图像和mysql中的图像路径

php - Mysql 查找两张表

java - 为什么不让 Domains 直接扩展 GenericDAO?

php - Symfony 从实体标题创建 slug

php - Controller 类上的@Security 注释被操作方法覆盖

c# - 在 C# 中实现树管理父子

java - java5 之前的版本 : Singleton pattern: Double checked Locking vs Static factory method

design-patterns - JavaFx 2.0 方 block 游戏动画/代码设计

php - PHP 中的 ORM 和 DAO

java - ORMLite 外国集合 : Must use ClosableIterator?