如何在 PHP 中使用 MVC 从结果集中获取额外属性
我有以下结构:程序>项目>>阶段
用于查看每个模型的视图文件非常标准,您将在视图文件中看到类似以下内容:
<?php foreach ($programs as $p):?>
<p><?php echo $p->getName()?></p>
<?php endforeach?>
我的 $programs 变量只是一个 Program 对象数组。
现在,举个例子,假设我有一个页面,其中将列出阶段及其父项目和程序的名称。我的 SQL 语句执行所需的连接,并且我的对象数组将包含程序和项目名称的属性。我已经向我的 Stage 模型添加了 2 个方法: getProjectName() 和 getProgramName(),但是,我不确定这是否是执行此操作的正确方法。如果我想在此页面上列出项目或程序的其他属性,那么我需要在 Stage 模型中创建一堆我认为不属于那里的额外方法。
任何人都可以提供有关如何最好地完成这种事情的任何见解吗?谢谢。
I have the following structure: Program > Project > Stage
My view file for viewing each of these models is pretty standard where you would see something like the following in the view file:
<?php foreach ($programs as $p):?>
<p><?php echo $p->getName()?></p>
<?php endforeach?>
My $programs variable is just an array of Program objects.
Now, as an example, let's say that I have a page that will list stages with the name of its parent project and program. My SQL statement does the required join and my array of objects will contain the properties of the Program and Project name. I have added 2 methods to my Stage model: getProjectName() and getProgramName(), but, I am not sure if this is the right way to go about doing this. What if I wanted to list other properties of projects or programs on this page, then I would need to make a bunch of extra methods in the Stage model which I don't think belong there.
Can anyone offer any insight as to how best to accomplish this sorta thing? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将获得包含所有字段的数组之类的数据
像这样的东西
You would get data like array with everything fields
something like this