简体   繁体   English

Zend和数据映射器-如何从多个表中获取数据

[英]Zend and data mappers - how to get data from multiple tables

I am newbie to Data Mappers concept and so far I only used Active Record. 我是Data Mappers概念的新手,到目前为止,我仅使用Active Record。

I have following MySQL tables: 我有以下MySQL表:

Calendar 日历

  • id ID
  • idEvent idEvent
  • date 日期

Events 大事记

  • id ID
  • name 名称

I need to fetch names and dates of all events that happened before today. 我需要获取今天之前发生的所有事件的名称和日期。

What's the most efficient structure to archive this? 归档此文件最有效的结构是什么? I am getting super confused if I need to create objects for each Calendar entry, and how will I link idEvent from calendar table and id from events table. 如果需要为每个Calendar条目创建对象,我将感到非常困惑,以及如何链接日历表中的idEvent和事件表中的id。

In yourProjectRootFolder/application/Module/models/mappers/CalendarMapper.php (amend the path as per your directory structure if you're not using the same): 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;
    }

Now, each time you call $calendarMapper->find($calendarId); 现在,每次调用$calendarMapper->find($calendarId); in your controller, you should be provided with both Calendar object with Event object linked to it. 在您的控制器中,应该为您提供两个Calendar对象以及与其链接的Event对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM