end和数据映射器——如何从多个表中获取数据


Zend and data mappers - how to get data from multiple tables

我是数据映射器概念的新手,到目前为止我只使用活动记录。

我有以下MySQL表:

日历

  • id
  • idEvent

  • id
  • <
  • 名称/gh>

我需要获取今天之前发生的所有事件的名称和日期。

最有效的归档结构是什么?我得到超级困惑,如果我需要为每个日历条目创建对象,以及我将如何从日历表和id从事件表链接idEvent。

yourProjectRootFolder/application/Module/models/mappers/CalendarMapper.php中(如果您不使用相同的目录结构,请修改路径):

<?php
class Module_Model_Mapper_Calendar extends Your_Abstract_Mapper
{
    public function rowToObject($row)
    {
        $calendar = new Module_Model_Calendar();
        $calendar->setDate($row->date);
        if(!empty($row->idEvent))
        {
            $eventMapper = new Module_Model_Mapper_Event();
            $event = $eventMapper->find($row->idEvent);
            $calendar->setEvent($event);
        }

        return $calendar;
    }

现在,每次在控制器中调用$calendarMapper->find($calendarId);时,您应该提供Calendar对象和Event对象链接到它。