如何在 PHP 中使用 MVC 从结果集中获取额外属性

发布于 2024-12-01 19:08:46 字数 529 浏览 5 评论 0原文

我有以下结构:程序>项目>>阶段

用于查看每个模型的视图文件非常标准,您将在视图文件中看到类似以下内容:

<?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 技术交流群。

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

发布评论

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

评论(1

痴意少年 2024-12-08 19:08:47

您将获得包含所有字段的数组之类的数据
像这样的东西

$programs = array(
  0 =>array(
    'name' => 'prog name 1',
    'somefield' =>'some prog field 1',
    'project' => array(
      0 =>array(
         'name' => 'proj name 1',
         'somefield' =>'some proj field 1',
         'stage'  => array(
           0=>array(
             'name'  =>'stage name 1',
             'somefield' =>'some stage field 1',
           ),  
           1=>array(
             'name'  =>'stage name 2',
             'somefield' =>'some stage field 2',
           )
         )
      ),
      1 =>array(
         'name' => 'proj name 2',
         'somefield' =>'some proj field 2',
         'stage'  => array(
           0=>array(
             'name'  =>'stage name 1',
             'somefield' =>'some stage field 1',
           ),  
           1=>array(
             'name'  =>'stage name 2',
             'somefield' =>'some stage field 2',
           )
         )
      )
    )
  )
);

You would get data like array with everything fields
something like this

$programs = array(
  0 =>array(
    'name' => 'prog name 1',
    'somefield' =>'some prog field 1',
    'project' => array(
      0 =>array(
         'name' => 'proj name 1',
         'somefield' =>'some proj field 1',
         'stage'  => array(
           0=>array(
             'name'  =>'stage name 1',
             'somefield' =>'some stage field 1',
           ),  
           1=>array(
             'name'  =>'stage name 2',
             'somefield' =>'some stage field 2',
           )
         )
      ),
      1 =>array(
         'name' => 'proj name 2',
         'somefield' =>'some proj field 2',
         'stage'  => array(
           0=>array(
             'name'  =>'stage name 1',
             'somefield' =>'some stage field 1',
           ),  
           1=>array(
             'name'  =>'stage name 2',
             'somefield' =>'some stage field 2',
           )
         )
      )
    )
  )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文