douan5151 2014-06-19 12:05
浏览 20
已采纳

循环遍历CakePHP中的相关数据

I am feeling my way through CakePHP, trying to replicate what I can already do in vanilla PHP.

I have 2 tables, events and eventlist. events contains general information about an event. eventlist contains information that appears as a bullet-pointed list on each event page.

eventlist consists of an id, an event_id which links each entry to the relevant event in the event table, and a string that is displayed on the page. In vanilla PHP, I would simply SELECT list_info FROM eventlist WHERE event_id = 1 or whatever, and loop through the resulting array.

However, I have no idea where to start with this in CakePHP, and I'm not sure how i'd search for it to find out.

I have already created an association between the two tables:

class Event extends AppModel{

    public $hasMany = array(
        'EventList'
        );

}

class EventList extends AppModel{

public $useTable = 'eventlist';

public $belongsTo = array(
    'Event'
);

}

And I have a simple view set up for Event:

<h2><?php echo $event['Event']['event_name'] ?></h2>

<p><?php echo $event['Event']['event_description']?></p>

<h3>£<?php echo $event['Event']['event_cost']?></p>

But now I have no idea what to do - I am new to CakePHP and MVC as a whole. I assume I need to do something in EventListController to load the data into an array. Any help appreciated.

  • 写回答

1条回答 默认 最新

  • dsg435665475 2014-06-19 12:17
    关注

    By default, the relationships that you have setup should return the associated data when you query for an event. There are a number of ways to get the data in a displayable format, such as using HTML helper to output an (un)ordered list. But, the example below should help.

    Also, a quick way to debug the results from finding a record in your controller is to use pr (an alias for print_r) like pr($event);exit;. There are other ways to debug too, but this is a quick and dirty method if you're working locally.

    Controller

    $event = $this->Event->findById(`event_id`);
    $this->set(compact('event'));
    

    View

    <h2><?php echo $event['Event']['event_name'] ?></h2>
    <p><?php echo $event['Event']['event_description']?></p>
    <p>£<?php echo $event['Event']['event_cost']?></p>
    <!-- Output each list item in a paragraph -->
    <p>
        <?php foreach($event['EventList'] as $listItem:
           echo $listItem['ListItemColumn'];
        endforeach; ?>
    </p>
    

    Again...many many ways to get and output this data, but the long and short is that the returned results should have an array key Event and an array key EventList with the data from each table stored within those keys.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题