Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » SS3 CustomSiteConfig Extension + has_many relation: visible in CMS but can't output to template

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 431 Views
  • vwd
    Avatar
    Community Member
    157 Posts

    SS3 CustomSiteConfig Extension + has_many relation: visible in CMS but can't output to template Link to this post

    Hi,

    I have a CustomSiteConfig extension which has a has_many relation to a DataObject. The GridField appears nicely in the CMS allowing me to add/remove/modify the DataObjects. However, I can't seem to output the list of DataObjects in the template when trying to access the list via SiteConfig directly, but only through an explicit ORM query.

    Event.php

    class Event extends DataObject {
       static $db = array(
          'EventTitle' => 'Varchar',
          'SortOrder' => 'Int'
       );
       
       public static $has_one = array(
          'EventLinkPage' => 'SiteTree',
          'MyCustomSiteConfig' => 'SiteConfig'
       );
       
       static $summary_fields = array(
          'EventTitle',
          'EventLinkPage.Title'
       );   
       
       public static $default_sort = "SortOrder ASC";
       
       function getCMSFields(){
          $fields = new FieldList(
             new TextField("EventTitle" , "Event Title"),
             new TreeDropdownField("EventLinkPageID","Link Page", 'SiteTree'));
          return $fields;      
       }
    }

    CustomSiteConfig.php

    class CustomSiteConfig extends DataExtension{

       static $db = array(
          'CompanyName' => 'Text'
       );
       
       static $has_many = array(
          'EventsList' => 'Event'
       );
       
       function updateCMSFields(FieldList $fields) {
          $myGridConfig = GridFieldConfig_RelationEditor::create();
          $myGridConfig->addComponent(new GridFieldSortableRows('SortOrder'));
          $myGridConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
             'EventTitle' => 'Event Title',
             'EventLinkPage.Title' => 'Event Page'
          ));

          $fields->addFieldToTab("Root.Events", new GridField("EventsList", "Add / Modify Events", Event::get()->sort('SortOrder'), $myGridConfig));
          return $fields;
       }   
       
       // this outputs the events list in the template correctly
       function DisplayEvents(){
          return Event::get();
       }   
    }

    events.ss

    <!-- This works correctly -->
    <ul id="eventsList">
       <% loop SiteConfig.DisplayCTAs %>
          <li>
             <a href="$EventLinkPage.URL>$EventTitle </a>
          </li>
       <% end_loop %>
    </ul>

    <!-- This doesn't work (but should?) -->
    <ul id="eventsList">
       <% loop SiteConfig.EventsList %>
          <li>
             <a href="$EventLinkPage.URL>$EventTitle </a>
          </li>
       <% end_loop %>
    </ul>

    In theory, I should just be able to use $SiteConfig.EventsList as I can every other member in CustomSiteConfig (eg. $SiteConfig.CompanyName) in the template.

    Any ideas why I'm only able to output the list when returning a DataList via an explicit ORM query?

    The resulting SQL Queries when using $SiteConfig.EventsList is:

    SHOW TABLES LIKE 'Event' 0.0004ms

    SHOW FULL FIELDS IN "Event" 0.002ms

    SELECT DISTINCT "ClassName" FROM "Event" 0.0002ms

    SELECT DISTINCT "Event"."ClassName", "Event"."Created", "Event"."LastEdited", "Event"."EventTitle", "Event"."SortOrder", "Event"."EventLinkPageID", "Event"."MyCustomSiteConfigID", "Event"."ID", CASE WHEN "Event"."ClassName" IS NOT NULL THEN "Event"."ClassName" ELSE 'Event' END AS "RecordClassName" FROM "Event" ORDER BY "Event"."SortOrder" ASC 0.0006ms

    Thanks very much.
    VWD.

    431 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.