映射单个对象还是多个对象?

发布于 2024-12-25 14:07:40 字数 390 浏览 5 评论 0原文

正如您可以想象的那样,涉及术语(任意组合)“多个、表格、对象”的搜索会返回很多页面,这些页面并不特定于我的特定问题 - 而且我不完全确定如何措辞,所以我将举一个例子:

假设我有这些表:“Users”、“Projects”和“ProjectNotes”。在任何给定的视图(使用 MVC 模式)上,我可能想要调用有关一个或多个项目的信息并显示有关其父级用户和子级注释的信息——或者为了拥有更具体的场景,假设我想要拥有一个显示有关单个项目及其子注释的信息以及可能对父用户的名字和姓氏的引用的视图。

在这种情况下,我应该映射一个包含所有必需数据的对象并将其传递给视图,还是应该映射特定项目,使用其“user_id”外键来映射适当的用户对象,映射该项目的所有子对象注释作为对象,并将这组对象发送到视图?

我感谢您花时间阅读本文。

As you can imagine, a search involving the terms (in any combination) "multiple, tables, objects" returns a lot of pages that are not specific to my particular question -- and I'm not entirely sure how to word it, so I will give an example:

Say I have these tables: 'Users', 'Projects', and 'ProjectNotes'. On any given view (working with an MVC pattern) I may want to call up information about one or more projects and display information about their parents users and child notes -- or for the sake of having a more specific scenario, let's say I want to have a view that displays information about a single project along with its child notes and maybe a reference to the parent user's first and last name.

In such a scenario, should I map one object which contains all required data and pass it to the view, or should I map the specific project, use its 'user_id' foreign key to map the appropriate user object, map all of the project's child notes as objects, and send this group of objects to the view?

I thank you for your time reading this.

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

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

发布评论

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

评论(1

唔猫 2025-01-01 14:07:40

让我觉得这是一个折腾,无论哪种方式似乎都很好。但我倾向于后者:将所有每条记录的数据组装到一个数组/对象中并将其传递给视图。

最终,您在操作中可以轻松使用哪些数据的具体细节取决于您的查询、ORM、查询/映射器/ORM 生成的对象图等。但是假设您的联接查询生成一个具有以下功能的项目: Note 和 User 的成员,您可以在操作中执行类似的操作:

$project = $repository->getProject(); // however this is done
$this->view->projectData = array(
   'name' => $project->name,
   'date' => $project->date,
   'user_name' => $project->User->name,
   'user_email' => $project->User->email,
   'note' => $project->Note->content,
);

也就是说,视图不需要了解对象图;这些知识存在于控制器/操作及以上级别。该操作从图表中解开数据并将其重新包装成简单的形式(数组)以供视图渲染。

您甚至可以将其推送到表示 $project 的中间视图模型对象中 - 该对象在构造函数中接受 $project 对象/图,但不可变且可读-仅有的。然后将此视图模型对象传递给视图进行渲染。

Strikes me as a toss up, either way seems fine. But I'd lean towards the latter: assemble all your per-record data into a single array/object and pass that to the view.

Ultimately, the specifics of what data is easily available to you in the action depends upon your queries, your ORM, the object graph that get produced by the queries/mappers/ORM, etc. But assuming that your joined query produces a Project that has members for Note and User, you could do something like this in your action:

$project = $repository->getProject(); // however this is done
$this->view->projectData = array(
   'name' => $project->name,
   'date' => $project->date,
   'user_name' => $project->User->name,
   'user_email' => $project->User->email,
   'note' => $project->Note->content,
);

That is, the view doesn't need to know about the object graph; this knowledge resides at the level of controller/action and above. The action unwraps the data from the graph and rewraps it into a simple form (array) for the view to render.

You could even push this down into an intermediate view-model object representing the $project - one that accepts the $project object/graph in the constructor, but is immutable and read-only. Then pass this view-model object to the view for rendering.

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