I am newbie to Data Mappers concept and so far I only used Active Record.
I have following MySQL tables:
Calendar
Events
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.
In yourProjectRootFolder/application/Module/models/mappers/CalendarMapper.php
(amend the path as per your directory structure if you're not using the same):
<?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);
in your controller, you should be provided with both Calendar
object with Event
object linked to it.