Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Getting related Objects [RESOLVED]

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 706 Views
  • VictorH
    Avatar
    Community Member
    29 Posts

    Getting related Objects [RESOLVED] Link to this post

    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 %>

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Getting related Objects [RESOLVED] Link to this post

    <% 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.

  • VictorH
    Avatar
    Community Member
    29 Posts

    Re: Getting related Objects [RESOLVED] Link to this post

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

    706 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.