Reputation: 157
I am very new in SilverStripe thats why, facing so many problem. I want to retrieve data from table and show them in a page template.I have created a modeladmin and from there i can insert records but i cant figure out how could i retrieve those data and show them in a page template? basically my codes are bellow....
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 %>
But I got nothing only if i write something into content on the page then the contents are coming. Any help would be really appreciated.
Upvotes: 1
Views: 987
Reputation: 3868
in the Page_Controller:
if SerialsCollection has only one instance make sure you get that instance alone, and make sure you use a get function:
public function getSerials(){
return SerialsCollection::get()->First();// for one item
return SerialsCollection::get();//for an array (use only one of those)
}
the get() alone will return a collection (ArrayList) which you will have to loop
in the Template if you used the First(one item):
$Serials.Title
will print the title or if you used the array:
<% loop $Serials %>
$Title
<% end_loop %>
Upvotes: 1
Reputation: 576
Read Querying Data from http://doc.silverstripe.org/en/developer_guides/model/data_model_and_orm/ .
In your case you would get data from the dataobjects with:
$sc = SerialsCollection::get();
Also you should read this: http://doc.silverstripe.org/en/developer_guides/model/lists/
Upvotes: 0