我在SilverStripe网站上有一个页面,按年使用GroupedList功能列出毕业生组:
当前(我不显示该组的年份范围)
2016
2015
这是GroupedList函数:
public function getGroupedMembers() {
$groupedList = GroupedList::create(
Member::get()
->filter("IsCurrent", False)
->sort("GraduationYear", "DESC")
);
return $groupedList;
}
它如何在模板上呈现:
<% loop $getGroupedMembers.GroupedBy(GraduationYear) %>
<div class="row">
<h3>$GraduationYearRange</h3>
<% loop $Children %>
<div class="col-xs-6 col-sm-6 col-md-4 col-lg-3 members-list">
<p class="member-name">$Name</p><br/>
</div>
<% end_loop %>
</div>
<% end_loop %>
等等。但是,我想展示开始和毕业年份,所以我有2015-2016,2014-2015,2013-2014等等。我创建了一个名为getGraduationYearRange
的函数,它位于我的Member
数据对象类中:
<?php
class Member extends DataObject
{
private static $db = array(
'Name' => 'Varchar(250)',
'IsCurrent' => 'Boolean',
'GraduationYear' => 'varchar(4)',
'Bio' => 'HTMLText',
);
private static $has_one = array(
'MemberImage' => 'Image',
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Main", new CheckboxField ("IsCurrent", "Current Member?"), "Bio");
$fields->addFieldToTab("Root.Main", new TextField("GraduationYear", "Graduation Year", '', 4), "Bio");
return $fields;
}
public function getGraduationYearRange(){
$startYear = date('Y', strtotime("-1 year", strtotime($this->GraduationYear)));
$gradYear = $this->GraduationYear;
echo $gradYear." - ".$startYear;
return $gradYear." - ".$startYear;
}
}
class MemberAdmin extends ModelAdmin {
private static $managed_models = 'Member';
private static $url_segment = 'Member';
private static $menu_title = 'Members';
}
getGraduationYearRange函数似乎不起作用。它在模板中不返回任何内容。 echo语句也不返回任何内容。
答案 0 :(得分:3)
我想我可能已经解决了!以下是关于getGraduationYearRange函数的更改内容:
public function getGraduationYearRange(){
$gradYear = $this->GraduationYear;
$startYear = $gradYear - 1;
return $startYear." - ".$gradYear;
}
我也按照3dgoo的建议做了,并将GraduationYearRange
放入GroupedBy函数中。这些变化似乎解决了这个问题,因为我现在看到了正确的年份范围。
即2015-2016,2014-2015,2013-2014等
感谢您的帮助,但是:)