开发者

How to grab extra properties from a result set using MVC in PHP

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 wil开发者_如何学运维l 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.


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',
           )
         )
      )
    )
  )
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜