Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Relation many to many between pages

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2959 Views
  • pga
    Avatar
    Community Member
    8 Posts

    Relation many to many between pages Link to this post

    Hi Everyone,

    I have to make relation "many to many" between two kind of pages:
    between products and applications pages.

    For this I created two files in mysite/code/ folder:

    mysite/code/ProductsPage.php file:

    class ProductsPage extends Page {
       static $db = array();   
       static $has_one = array();
       static $belongs_many_many = array('Applications' => 'AppsPage');
       function getCMSFields()
       {
          $fields = parent::getCMSFields();   
          $applicationList = DataObject::get('AppsPage');
          $fields->addFieldToTab('Root.Content.Applications', new CheckboxSetField('Applications', '', $applicationList));
          return $fields;
       }
    }
    class ProductsPage_Controller extends Page_Controller {
       function ShowApps()
       {      
          return DataObject::get('AppsPage', "ParentID = {$this->ID}");
       }
    }

    mysite/code/AppsPage.php file:

    class AppsPage extends Page {
       static $db = array();
       static $has_one = array();
       static $many_many = array('Products' => 'ProductsPage');
       function getCMSFields()
       {
          $fields = parent::getCMSFields();
          $productList = DataObject::get('ProductsPage');
          $fields->addFieldToTab('Root.Content.Products', new CheckboxSetField('Products', '', $productList));
          return $fields;
       }
    }
    class AppsPage_Controller extends Page_Controller {
       function ShowProducts()
       {
          return DataObject::get('ProductsPage', "ParentID = {$this->ID}");
       }
    }

    Almost everything above works perfectly. I can select multiple products for application,
    and I see my selections in the products settings when I use administration panel.

    Question is - how to display list of Products on Application site and list of Applications on
    product site. Unfortunately defined by me ShowProducts() and ShowApps() methods doesn't work.

    I've based on:
    http://doc.silverstripe.com/doku.php?id=tutorial:5-dataobject-relationship-management
    http://doc.silverstripe.com/doku.php?id=datamodel&s=relation
    http://doc.silverstripe.com/doku.php?id=recipes:many_many-example

    Unfortunately examples on websites above are little different. There is no examples
    of relation many to many between pages, only between pages and some abstract categories.

    Could anybody help me how to display data from relation in template?

    Thank you,
    Pawel

  • blueskies
    Avatar
    Community Member
    42 Posts

    Re: Relation many to many between pages Link to this post

    Try:

    function ShowApps(){
       return $this->getManyManyComponents('Applications');
    }

    function ShowProducts()   {
       return $this->getManyManyComponents('Products');
    }

    Note the reference to Applications and not AppsPage (from: static $belongs_many_many = array('Applications' => 'AppsPage');)

    getManyManyComponents makes the magic happen to get stuff and spit it out to the template. Then you can have a regular control in your template like so:

    <% control ShowApps%>
    $Title<br />
    $Link
    <% end_control %>

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Relation many to many between pages Link to this post

    or even simpler: use $Applications and $Products directly in your templates,
    and spare youself the Show*() functions ;)

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