Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Linking DataObject to multiple page types

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: 679 Views
  • borriej
    Avatar
    Community Member
    267 Posts

    Linking DataObject to multiple page types Link to this post

    Hello,

    pretty basic question, but i've only worked with has_one relations.

    I have a dataobject linked to a page with a has_one relation like, but now i want to link the dataobject to two, or in the future three, different page types. The idea: I don't want a relation between the files under PageTypeA & PageTypeB. I just want to use the Video.php code again. So when i create a PageTypeB, i don't want to see the files in the manager that were created under PageTypeA.

    The DataObject:

    <?php
    class Video extends DataObject
    {
       static $db = array (
          'Title' => 'Varchar(100)',
          'Width' => 'Varchar(100)',
          'Height' => 'Varchar(100)',
          'Description' => 'Text'
       );
       
       static $has_one = array (
          // Make sure this comes first
          'PageTypeA' => 'PageTypeA',
          'File' => 'File'
       );
       
    }

    The PageTypeA:

    <?php
    class PageTypeA extends Page {
       static $db = array(
          );
       
       static $has_one = array(
       );
       
       static $has_many = array (
          'Videos' => 'Video'
       );

       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab("Root.Content.Videos", new FileDataObjectManager(
             $this,
             'Videos', // relation name
             'Video', // class name of the DataObject
             'File', // name of the file relation in the DataObject
             array('Title' => 'Title','Width' => 'Width','Height' => 'Height','Description' => 'Description'),
             new FieldSet(
                new TextField('Title'),
                new TextField('Width'),
                new TextField('Height', 'Height'),
                new TextareaField('Description')
             )
          ));
          return $fields;
       }
    }
    class PageTypeA_Controller extends Page_Controller {
    }

    Now i want to link this DataObject as well to PageTypeB.
    How do i do this?

    Like this?
    The DataObject:

    <?php
    class Video extends DataObject
    {
       static $db = array (
          'Title' => 'Varchar(100)',
          'Width' => 'Varchar(100)',
          'Height' => 'Varchar(100)',
          'Description' => 'Text'
       );
       
       static $has_one = array (
          'File' => 'File'
       );
       static $has_many = array (
          'PageTypeA' => 'PageTypeA',
          'PageTypeB' => 'PageTypeA',
       );

       
    }

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Linking DataObject to multiple page types Link to this post

    To clear things up, do you want to link one video to several different pages or do you want to just link the relationship to several different page types but the video still only belongs to one page?

    If you only want video to belong to one page at a time then continue using has_one. In your last code snippet it should read:

    <?php
    class Video extends DataObject
    {
       static $db = array (
          'Title' => 'Varchar(100)',
          'Width' => 'Varchar(100)',
          'Height' => 'Varchar(100)',
          'Description' => 'Text'
       );

       static $has_one = array (
          'PageTypeA' => 'PageTypeA',
          'PageTypeB' => 'PageTypeB',
          'File' => 'File'
       );
    }

    If however you want to link one video to several pages and vice versa you have to use $many_many instead. Another option is to let all the pages that have a video relation descend from the same parent. In that case all the sub classes get the same relationship as the parent.

    For example

    <?php
    class VideoPage extends Page
    {
       static $has_many = array (
          'Videos' => 'Video'
       );
       ...
    }
    ?>

    Then you just sub class VideoPage for the pages that need to use the video.

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