I have three objects – Calendar, Event and Occurrence. A calendar has many events and each event has many occurrences. Now in CalendarMapper, I want to create a calendar object that automatically populates the events array inside, and each event’s occurrences are also automatically populated.
I read about getParentRow and tried something like this in Application_Model_CalendarMapper:
public function fetchAllWithEvents($id)
{
$resultSet = $this->getDbTable()->fetchAll();
$entries = array();
foreach ($resultSet as $row) {
$entry = new Application_Model_Calendar();
$events = $row->findParentRow('Application_Model_DbTable_Events');
...
The above line however does not seem to go through EventMapper (atleast not fetch/fetchAll), so it just gets an array containing the events.
So, how do I insert the occurrence data into each event here?
Is the correct way to call EventsMapper directly (and EventsMapper uses OccurrenceMapper to populate Occurrences)?
Does that mean the referenceMap is not of much use in this context?
$entry->setEvents($events);
I want to do this, but the $events should be an array of Event objects which it currently is not.
The Dbtable classes
Calendar:
class Application_Model_DbTable_Calendars extends Zend_Db_Table_Abstract
{
protected $_name = 'phpc_calendars';
protected $primary_key = 'cid';
protected $_referenceMap = array (
'Events'=> array (
'columns'=>'cid',
'refTableClass'=>'Application_Model_DbTable_Events',
'refColumns'=>'cid'
)
);
}
Events:
class Application_Model_DbTable_Events extends Zend_Db_Table_Abstract
{
protected $_name = 'phpc_events';
protected $primary_key = 'eid';
protected $_dependentTables = array ('Calenars');
protected $_referenceMap = array (
'occurrences'=> array (
'columns'=>'eid',
'refTableClass'=>'Application_Model_DbTable_occurrences',
'refColumns'=>'eid'
)
);
}
Occurrences:
class occurrences extends Zend_Db_Table_Abstract
{
protected $_name = 'phpc_occurrences';
protected $primary_key = 'ocid';
protected $_dependentTables = array ('Events');
}
Could not find a satisfactory way to do this. So, I implemented it myself without using referenceMaps