2016-03-24 5 views
0

DashboardPagePastEventデータオブジェクトには$has_manyの関係があります。私のDashboardPage.ssテンプレートでは、すべてPastEventsをループすることができます。私はので、私はDashboardPage.ssテンプレートに次のことを行うことができるはず$ has_manyの関係を信じる:

DashboardPage.ss

<% loop $PastEvents %> 
    <div> 
     <div>$EventName</div> 
     <div>$ClassType</div> 
     etc... 
    </div> 
<% end_loop %> 

しかし、何も表示されません。私は何が欠けていますか?

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'); 
    } 

} 

PastEvent.phpあなたはあのから過去のイベントを追加することができます

public function getCMSFields() { 
... 
    $fields->push(GridField::create('PastEvents', 'Past Events', 
     $this->PastEvents(), 
     singleton('PastEvent')->canEdit() 
      ? GridFieldConfig_RecordEditor::create() 
      : GridFieldConfig_RecordViewer::create() 
    )); 
... 
} 

DashboardPageにPastEventを管理する必要が

<?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); 
    } 

} 
+0

ifusion、DashboardPageとPastEventの関係をどのように設定していますか? – muskie9

+0

@ muskie9 - DashboardPageへのPastEventオブジェクトには$ has_one、DashboardPageには$ has_manyがあります。$ PastEvents => $ PastEvent' – ifusion

+0

@ifusionここで、 'PastEvent'sを' DashboardPage'に追加して ' PastEvent'は 'DashboardPage'に関連していますか? –

答えて

2

あなたのページを '/ dashboard' URLで作成したと仮定して、場所を指定してください。

$dashboard = SiteTree::get_by_link('dashboard'); 
$events = $dashboard->PastEvents(); 
$events->add($this->createNewEvent()); 

あなたは(暗黙のindex()アクションがデフォルトで使用されている)DashboardPageを表示するために、あなたのshow()アクションは必要ありません。ページアクションはページ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 %> 

あなたのページには、showアクション

public function Link() { 
    return $this->DashboardPage()->Link('show/'.$this->ID); 
} 

への適切なリンクを提供していますのでごshowアクションは、この

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')); 
} 
ようなものかもしれません
+0

ニース!あなたの助けをありがとう:)それは今働いています。私はPastEventsのページとしてデータオブジェクトを作成しようとしていました(申し訳ありませんが、私の質問に入れておくべきです)。それで、私はPastEventsデータオブジェクトにLink()メソッドを持っていたのです。私はこのチュートリアルに従っていました:https://www.silverstripe.org/learn/lessons/controller-actions-dataobjects-as-pages - 私はRegionsPage.php = DashboardPage.phpやRegion.php = PastEventなどの名前を変更しました。 PHP – ifusion

関連する問題