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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Getting related Objects [RESOLVED]


Reply


3 Posts   979 Views

Avatar
VictorH

Community Member, 29 Posts

11 August 2011 at 7:29am

I can't figure out how to display the "Resources Link" under there appropriate "Resource Link Category" any help?

Here's the code.

ResourceLinkCategory.php

class ResourceLinkCategory extends DataObject {
   static $db = array('Name' => 'Text');
   
   static $has_many = array('ResourceLinks' => 'ResourceLink');

   function getCMSFields_forPopup() {
      $fields = new FieldSet();

      $fields->push (new TextField('Name'));

      return $fields;
   }
}

ResourceLink.php

class ResourceLink extends DataObject {
   static $db = array(
      'URL' => 'Text',
      'Description' => 'Text'
   );
   
   static $has_one = array(
      'ResourceLinkPage' => 'ResourceLinkPage',
      'MyResourceLinkCategory' => 'ResourceLinkCategory'
   );

   function getCMSFields_forPopup() {
      $fields = new FieldSet();

      $fields->push (new TextField('URL'));
      $fields->push (new TextareaField('Description'));
      $fields->push (new HasOneDataObjectManager(
       $this,
       'MyResourceLinkCategory',
       'ResourceLinkCategory',
       array('Name' => 'Name'),
       'getCMSFields'
      ));

      return $fields;
   }
}

ResourceLinkPage.php

class ResourceLinkPage extends Page {
   static $has_many = array(
      'ResourceLinks' => 'ResourceLink',
      'ResourceLinkCategories' => 'ResourceLinkCategory'
   );
   
   function getCMSFields() {
      $fields = parent::getCMSFields();
      
      $resourcelinks = new DataObjectManager(
         $this,
         'ResourceLinks',
         'ResourceLink',
         array(
            'URL' => 'URL',
            'Description' => 'Description'
         ),
         'getCMSFields_forPopup'
      );
      $fields->addFieldToTab('Root.Content.ResourceLinks', $resourcelinks);
      
      return $fields;
   }
}

class ResourceLinkPage_Controller extends Page_Controller {
   function GetResourceLinkCategories() {
      return DataObject::get('ResourceLinkCategory', '', '', '', '');
   }
}

ResourceLinkPage.ss

<% control GetResourceLinkCategories %>
   <h2><span>$Name</span></h2>
<-- Display Resources Link related to the Category here -->
<% end_control %>

Avatar
MarcusDalgren

Community Member, 288 Posts

11 August 2011 at 9:38am

<% control GetResourceLinkCategories %>
   <h2><span>$Name</span></h2>
   <% control ResourceLinks %>
   <!-- Resource link stuff here -->   
   <% end_control %>
<% end_control %>

There's always a method available with the same name as the relationship which acts as a getter for all related objects.

Avatar
VictorH

Community Member, 29 Posts

11 August 2011 at 10:00am

*sigh* I need some sleep. Thanks that works just like it should.