我是SilverStripe的新手,这就是为什么,面对这么多问题。我想从表中检索数据并在页面模板中显示它们。我创建了一个modeladmin,从那里我可以插入记录,但我无法弄清楚如何检索这些数据并在页面模板中显示它们?基本上我的代码都在下面......
mysite/code/SerialsCollection.php
<?php
class SerialsCollection extends DataObject {
private static $db = array(
'Title' => 'Varchar',
'Author' => 'Varchar',
'Publisher' => 'Varchar',
'PublicationYear' => 'Date',
);
private static $searchable_fields = array(
'Title',
'Author'
);
private static $field_labels = array(
'Title' => 'Title' // renames the column to "Cost"
);
private static $summary_fields = array(
'Title',
'Author',
'Publisher',
'PublicationYear',
);
public function canView($member = null) {
return Permission::check('CMS_ACCESS_MyAdmin', 'any', $member);
}
public function canEdit($member = null) {
return Permission::check('CMS_ACCESS_MyAdmin', 'any', $member);
}
public function canDelete($member = null) {
return Permission::check('CMS_ACCESS_MyAdmin', 'any', $member);
}
public function canCreate($member = null) {
return Permission::check('CMS_ACCESS_MyAdmin', 'any', $member);
}
}
and mysite/code/SerialsCollectionAdmin.php ...
<?php
class SerialsCollectionAdmin extends ModelAdmin {
private static $managed_models = array(
'SerialsCollection'
);
private static $url_segment = 'serials-collection';
private static $menu_title = 'Serials Collection';
public function getList() {
$list = parent::getList();
return $list;
}
now, I am able to insert new records, view all and edit particular record. But what i want to create a page template and show these records on that page. I have tried it this way....
mysite/code/SerialsCollectionPage.php
<?php
Class SerialsCollectionPage extends Page{
}
Class Serials_Collection_Page_Controller extends Page_controller{
public function SerialsCollections()
{
return DataObject::get("SerialsCollection");
}
}
themes/SLIS/templates/SerialsCollectionPage.ss
<% include Header %>
<div id="Content">
<h1>This is SerialsCollection Page</h1>
<% control SerialsCollection %>
<p>Title: $Title</p>
<p>Author: $Author</p>
<p>Publication Year:$PublicationYear</p>
<% end_control %>
</div>
<% include Footer %>
但是,如果我在页面上的内容中写入内容,那么我什么都没有,那么内容即将到来。 任何帮助都会非常感激。
答案 0 :(得分:1)
如果SerialsCollection只有一个实例,请确保单独获取该实例,并确保使用get函数:
public function getSerials(){
return SerialsCollection::get()->First();// for one item
return SerialsCollection::get();//for an array (use only one of those)
}
单独的get()将返回一个你必须循环的集合(ArrayList)
模板中的如果您使用了First(一项):
$Serials.Title
将打印标题或者如果您使用了数组:
<% loop $Serials %>
$Title
<% end_loop %>
答案 1 :(得分:0)
从http://doc.silverstripe.org/en/developer_guides/model/data_model_and_orm/读取查询数据。
在您的情况下,您将从数据对象获取数据:
$sc = SerialsCollection::get();
另外,您应该阅读:http://doc.silverstripe.org/en/developer_guides/model/lists/