Silverstripe 循环使用$has\u许多关系?
我在我的Silverstripe 循环使用$has\u许多关系?,silverstripe,Silverstripe,我在我的仪表板页面上有一个$与我的粘贴事件数据对象的关系。在我的DashboardPage.ss模板中,我希望能够循环浏览所有的粘贴事件。我相信由于$has\u许多关系,我应该能够在仪表板页面.ss模板上执行以下操作: 仪表板页面.ss <% loop $PastEvents %> <div> <div>$EventName</div> <div>$ClassType</div>
仪表板页面
上有一个$与我的粘贴事件
数据对象的关系。在我的DashboardPage.ss模板中,我希望能够循环浏览所有的粘贴事件。我相信由于$has\u许多关系,我应该能够在仪表板页面.ss
模板上执行以下操作:
仪表板页面.ss
<% loop $PastEvents %>
<div>
<div>$EventName</div>
<div>$ClassType</div>
etc...
</div>
<% end_loop %>
$EventName
$ClassType
等
然而,什么也没有显示出来。我错过了什么
DashboardPage.php
<?php
class DashboardPage extends Page implements TemplateGlobalProvider {
private static $db = array(
'Testing' => 'Varchar(255)'
);
private static $has_many = array(
'PastEvents' => 'PastEvent'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeByName('Content');
return $fields;
}
public static function get_template_global_variables() {
return array('DashboardLink' => 'getDashboardLink');
}
public static function getDashboardLink($action='') {
$page = DashboardPage::get()->first();
return $page ? $page->Link($action) : '';
}
}
class DashboardPage_Controller extends Page_Controller {
private static $allowed_actions = array(
'show'
);
public function init() {
parent::init();
Requirements::css('themes/' . SSViewer::current_theme() . '/owl-carousel/owl.carousel.css');
Requirements::css('themes/' . SSViewer::current_theme() . '/owl-carousel/owl.theme.css');
Requirements::css('themes/' . SSViewer::current_theme() . '/owl-carousel/owl.transitions.css');
}
public function show(){
dd('Coming here');
}
}
<?php
class PastEvent extends DataObject {
private static $db = array(
'EventName' => 'Varchar(255)',
'ClassType' => 'Varchar(255)',
'Instructor' => 'Varchar(255)',
'EmbedCode' => 'Text',
'EventDate' => 'Date',
'Time' => 'Time'
);
private static $has_one = array(
'BranchLocation' => 'BranchLocation',
'DashboardPage' => 'DashboardPage'
);
private static $summary_fields = array(
'EventName' => 'EventName',
'BranchLocation.Name' => 'Branch Location',
'ClassType' => 'ClassType',
'Instructor' => 'Instructor',
'EventDate' => 'Event Date',
'Time' => 'Time',
);
public function getCMSFields() {
$fields = new FieldList(
TextField::create('EventName'),
TextField::create('ClassType'),
TextField::create('Instructor'),
DropdownField::create(
'BranchLocationID',
'Branch Location',
BranchLocation::get()->map('ID', 'Name')->toArray()
),
TextareaField::create('EmbedCode'),
DateField::create('EventDate')->setConfig('showcalendar', true)->setDescription('Click inside textbox to open calender'),
TimePickerField::create('Time')
);
return $fields;
}
public function Link() {
return $this->DashboardPage()->Link('show/'.$this->ID);
}
}
您需要在仪表板页面上管理PasteEvent
public function getCMSFields() {
...
$fields->push(GridField::create('PastEvents', 'Past Events',
$this->PastEvents(),
singleton('PastEvent')->canEdit()
? GridFieldConfig_RecordEditor::create()
: GridFieldConfig_RecordViewer::create()
));
...
}
假设您使用“/dashboard”url创建了页面,则可以从其他位置添加过去的事件
$dashboard = SiteTree::get_by_link('dashboard');
$events = $dashboard->PastEvents();
$events->add($this->createNewEvent());
显示仪表板页面不需要show()
操作(默认情况下使用隐式index()
操作)。页面操作扩展了页面url,所以当您请求/dashboard/show
时,您的操作将被调用,这似乎是您的意图
改进模板以显示没有事件:
<% if $PastEvents.Count %>
<ul>
<% loop $PastEvents %>
<li>
<a href="$Link">$EventName</a>
<div>$ClassType</div>
...
</li>
<% end_loop %>
</ul>
<% else %>
<p>There are no past events.</p>
<% end_if %>
你的表演可能是这样的
public function show($eventID = 0) {
$event = $this->PastEvents()->byID($eventID);
if (!$event) {
$this->httpError(404);
}
// render with templates/Page.ss and templates/Layout/PastEventDetails.ss
return $event->renderWith(array('PastEventDetails', 'Page'));
}
您需要在仪表板页面上管理PasteEvent
public function getCMSFields() {
...
$fields->push(GridField::create('PastEvents', 'Past Events',
$this->PastEvents(),
singleton('PastEvent')->canEdit()
? GridFieldConfig_RecordEditor::create()
: GridFieldConfig_RecordViewer::create()
));
...
}
假设您使用“/dashboard”url创建了页面,则可以从其他位置添加过去的事件
$dashboard = SiteTree::get_by_link('dashboard');
$events = $dashboard->PastEvents();
$events->add($this->createNewEvent());
显示仪表板页面不需要show()
操作(默认情况下使用隐式index()
操作)。页面操作扩展了页面url,所以当您请求/dashboard/show
时,您的操作将被调用,这似乎是您的意图
改进模板以显示没有事件:
<% if $PastEvents.Count %>
<ul>
<% loop $PastEvents %>
<li>
<a href="$Link">$EventName</a>
<div>$ClassType</div>
...
</li>
<% end_loop %>
</ul>
<% else %>
<p>There are no past events.</p>
<% end_if %>
你的表演可能是这样的
public function show($eventID = 0) {
$event = $this->PastEvents()->byID($eventID);
if (!$event) {
$this->httpError(404);
}
// render with templates/Page.ss and templates/Layout/PastEventDetails.ss
return $event->renderWith(array('PastEventDetails', 'Page'));
}
ifusion,如何设置仪表板页面和PasteEvent之间的关系?@muskie9-仪表板页面的PasteEvent对象上有一个s$has,仪表板页面上有多个s$has,$PasteEvents=>$PasteEvent
@ifusion在哪里添加PasteEvent
s到仪表板页面
来决定使用什么PasteEvent
s与仪表板页面相关的,如何设置仪表板页面和PasteEvent之间的关系?@muskie9-仪表板页面的PasteEvent对象上有一个s$has,仪表板页面上有多个s$has,$PasteEvents=>$PasteEvent
@ifusion在哪里添加PasteEvent
s到仪表板页面
来决定使用什么PasteEvent
s是否与仪表板页面相关?很好!谢谢你的帮助:)现在开始工作了。我试图创建一个dataobject作为PasteEvents的页面(很抱歉,我应该把它放在我的问题中)。这就是我在PasteEvents数据对象上使用Link()方法的原因。我遵循本教程:-我刚刚更改了名称,例如RegionsPage.php=DashboardPage.php和Region.php=PastEvent.phpNice!谢谢你的帮助:)现在开始工作了。我试图创建一个dataobject作为PasteEvents的页面(很抱歉,我应该把它放在我的问题中)。这就是我在PasteEvents数据对象上使用Link()方法的原因。我遵循本教程:-我刚刚更改了名称,例如RegionsPage.php=DashboardPage.php和Region.php=PastEvent.php