Jump to:

3373 Posts in 999 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [Solved] many to many relationship on same page type, ie article page has some other related article

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

Page: 1
Go to End
Author Topic: 953 Views
  • Ben_W
    Avatar
    Community Member
    79 Posts

    [Solved] many to many relationship on same page type, ie article page has some other related article Link to this post

    In the past, I have done one to many or many to many relationship using hasmanycomplextablefield, ManyManyComplexTableField. However, is it possible to assign the same page type to itself to form a many to many relationship. For instance you create a page type named. 'Article page' then in its own class add many to many relationship.

    <?php
    /**
    * Defines the ArticlePage page type
    */

    class ArticlePage extends Page {
       static $db = array(
          'Headline' => 'Text',
          'Summary' => 'Text'
       );
       static $has_one = array(
          
       );
       static $has_many = array(
          'MyArticlePages' => 'ArticlePage',
          'RelateToArticlePage' => 'ArticlePage'
    );
       
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $fields->addFieldToTab('Root.Content.Main', new TextField('Headline'), 'Content');
          $fields->addFieldToTab('Root.Content.Main', new TextField('Summary'), 'Content');
                      
          $tablefield = new ManyManyComplexTableField(
             $this,
             'MyArticlePages',
             'ArticlePage',
             array(
                'Headline' => 'Page Headline',
                'Summary' => 'Summary'
             ),
             'getCMSFields_forPopup',
              null,
              'Headline'
          );
          $tablefield->setAddTitle( 'A Related Page' );
          
          $tablefield->setPermissions(array(
          ));

          
          $fields->addFieldToTab( 'Root.Content.Related', $tablefield );
                
          return $fields;
       }
       
    }

    class ArticlePage_Controller extends Page_Controller {
       
    }
    ?>

    I got the following error:

    Fatal error: Cannot instantiate abstract class Object in /var/www/vhosts/isnt-test.com/httpdocs/sapphire/core/Object.php on line 229

    Has anyone done this before? thank you for taking the time to read this.

  • Ben_W
    Avatar
    Community Member
    79 Posts

    Re: [Solved] many to many relationship on same page type, ie article page has some other related article Link to this post

    I managed to make this work. the previous code mixed up with HasManyComplexTableField, my bad. For those of you might come cross the same issue. Here is the working code:

    <?php
    /**
    * Defines the RegionChildPage page type
    */

    class RegionChildPage extends Page {
       static $db = array(
          'Headline' => 'Text',
          'Summary' => 'Text'
       );
       static $has_one = array(
          
       );
       static $many_many = array(
          'MyRegionChildPages' => 'RegionChildPage'
    );
       
       static $belongs_many_many = array(
          'RelateToRegionChildPage' => 'RegionChildPage'
       );
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $fields->addFieldToTab('Root.Content.Main', new TextField('Headline'), 'Content');
          $fields->addFieldToTab('Root.Content.Main', new TextField('Summary'), 'Content');
                      
          $tablefield = new ManyManyComplexTableField(
             $this,
             'MyRegionChildPages',
             'RegionChildPage',
             array(
                'Headline' => 'Page Headline',
                'Summary' => 'Summary'
             ),
             'getCMSFields_forPopup',
              null,
              'Headline'
          );
          $tablefield->setAddTitle( 'A Related Page' );
          
          $tablefield->setPermissions(array(
          ));

          
          $fields->addFieldToTab( 'Root.Content.Related', $tablefield );
                
          return $fields;
       }
       
    }

    class RegionChildPage_Controller extends Page_Controller {
       
    }
    ?>

  • Ben_W
    Avatar
    Community Member
    79 Posts

    Re: [Solved] many to many relationship on same page type, ie article page has some other related article Link to this post

    Add this in as the filter if don't want the current region child page show up in the complexTableField as a option to link to itself.

    "RegionChildPage.ID != $this->ID", //To the filter section.

    Here is the full view of the ManyManyComplexTableField

    $tablefield = new ManyManyComplexTableField(
             $this,
             'MyRegionChildPages',
             'RegionChildPage',
             array(
                'Headline' => 'Page Headline',
                'Summary' => 'Summary'
             ),
             'getCMSFields_forPopup',
              "RegionChildPage.ID != $this->ID",
              'Headline'
          );

  • marc79
    Avatar
    Community Member
    65 Posts

    Re: [Solved] many to many relationship on same page type, ie article page has some other related article Link to this post

    Hi,

    Just searching around and found your post which helped me out with a problem I was having.

    "RegionChildPage.ID != $this->ID",

    I am using the above line opposite way, so that I only see certain information that has been assigned an ID, but I've since been looking for a way to add in NULL so that I can show items with a specific ID and those that are yet to be assigned and ID.

    Do you know if this is possible? I've tried several variations along the line of the below. e.g. with or without " " or ' ' but with no luck.

    "TrackProductID='$this->ID' or 'NULL'"

    Any suggestions would be greatly received.

    Thanks

    Marc

  • Ben_W
    Avatar
    Community Member
    79 Posts

    Re: [Solved] many to many relationship on same page type, ie article page has some other related article Link to this post

    Try this:
    "TrackProductID=$this->ID OR TrackProductID IS NULL"

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