php - SilverStripe data not showing in the template -
i tried can not output existing data in database server.
page.ss (layout directory)
<div class="content-container unit size3of4 lastunit"> <article> <div> <% loop $latestmydata %> $data1 <% end_loop %> </div> <div class="content">$content</div> </article> $form $commentsform </div>
mydata.php (mysite/code directory)
class mydata extends dataobject { private static $db = array( 'data1' => 'varchar(30)' ); } class mydata_controller extends controller { public function latestmydata($count = 1) { return mydata::get() ->limit($count); } }
i tried build database /dev/build
, flush cache ?flush=all
still no output. know wrong?
the latestmydata
function should in page_controller
instead of mydata_controller
controller page template call.
the mydata
object needs controlled in cms in way. can done either through modeladmin or gridfield relationship page.
here working example showing 1 way using 1 many relationship between page
, dataitem
:
dataitem.php
class dataitem extends dataobject { private static $db = array( 'title' => 'varchar(30)' ); private static $has_one = array( 'page' => 'page' ); }
page.php
class page extends sitetree { private static $has_many = array( 'dataitems' => 'dataitem' ); public function getcmsfields() { $fields = parent::getcmsfields(); $dataitemsfield = gridfield::create( 'dataitems', 'data items', $this->dataitems(), gridfieldconfig_recordeditor::create() ); $fields->addfieldtotab('root.dataitems', $dataitemsfield); return $fields; } } class page_controller extends contentcontroller { function latestdataitems($limit = 1) { return $this->dataitems()->limit($limit); } }
templates/layout/page.ss
<div class="content-container unit size3of4 lastunit"> <article> <div> <% loop $latestdataitems %> $title <% end_loop %> </div> <div class="content">$content</div> </article> $form $commentsform </div>
if want first dataitem
object can can call $dataitems.first
instead of our latestdataitems
function:
templates/layout/page.ss
<div class="content-container unit size3of4 lastunit"> <article> <% if $dataitems.first %> <% $dataitems.first %> <div> $title </div> <% end_with %> <% end_if %> <div class="content">$content</div> </article> $form $commentsform </div>
Comments
Post a Comment