Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

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


Reply


643 Views

Avatar
vwd

Community Member, 160 Posts

12 March 2013 at 2:52pm

Edited: 12/03/2013 2:54pm

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.