[英]Zend and data mappers - how to get data from multiple tables
我是Data Mappers概念的新手,到目前為止,我僅使用Active Record。
我有以下MySQL表:
日歷
大事記
我需要獲取今天之前發生的所有事件的名稱和日期。
歸檔此文件最有效的結構是什么? 如果需要為每個Calendar條目創建對象,我將感到非常困惑,以及如何鏈接日歷表中的idEvent和事件表中的id。
在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
對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.