如何在 Zend 中将引用映射链接到 DataMapper?
我有三个对象 - 日历、事件和事件。日历有许多事件,每个事件都有多次发生。现在在 CalendarMapper 中,我想创建一个日历对象,自动填充内部的事件数组,并且每个事件的发生也会自动填充。
我读到了有关 getParentRow 的内容,并在 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');
...
但是上面的行似乎没有经过 EventMapper (至少不是 fetch/fetchAll),因此它只是获取包含事件的数组。
那么,如何将发生数据插入到每个事件中呢? 直接调用EventsMapper(并且EventsMapper使用OccurrenceMapper来填充Occurrences)的正确方法是吗?
这是否意味着引用映射在这种情况下没有多大用处?
$entry->setEvents($events);
我想这样做,但 $events 应该是 Event 对象的数组,但目前不是。
Dbtable 类
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');
}
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');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法找到令人满意的方法来做到这一点。所以,我自己实现了它,没有使用referenceMaps
Could not find a satisfactory way to do this. So, I implemented it myself without using referenceMaps