Jump to:

3431 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Problem creating TreeMultiselectField to look up Page

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

Page: 1 2
Go to End
Author Topic: 4370 Views
  • Aaron Brockhurst
    Avatar
    Community Member
    30 Posts

    Problem creating TreeMultiselectField to look up Page Link to this post

    Hi

    In the Page object I need TreeMultiselectField that will allow the users to select more than one Page which can then be listed in the template as a link list.

    I've tried setting up an many_many relationship without success.

    When I put the following code in the database refuses to rebuild

    public static $many_many = array(
    'LinkPages' => 'Page'
    }

    Is there a better way of doing this?

    Aaron

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Problem creating TreeMultiselectField to look up Page Link to this post

    What you'll need to do is define the end of the relationship as well. So, because a Page can have many of itself both ways, you'll want something like this:

    public static $many_many = array(
       'LinkedPages' => 'Page'
    );

    public static $belongs_many_many = array(
       'BelongingPages' = 'Page'
    );

    Kinda confusing, but that should solve your database build problem.

  • Mo
    Avatar
    Community Member
    505 Posts

    Re: Problem creating TreeMultiselectField to look up Page Link to this post

    I am having an issue with this also. For me "/dev/build" works fine and everythjing looks normal, but whenever I try to save the data in the admin area, I get an "Error saving page" error.

    Currently my code is in a class "YourWorldHub":

    public static $many_many = array(
       'Box1Links'   => 'YourWorldHub'
    );

    public static $belongs_many_many = array(
       'BelongingPages' => 'YourWorldHub'
    );

    function getCMSFields() {
          $fields = parent::getCMSFields();

          $fields->addFieldToTab( 'Root.Content.CopyBoxes', new TreeMultiselectField( 'Box1LinksID', 'Possible Links', 'SiteTree' ) );

          return $fields;
    }

    I also tried:

    public static $many_many = array(
       'Box1Links'   => 'SiteTree'
    );

    Anyone have any ideas?

    Ta,

    Mo

  • Mo
    Avatar
    Community Member
    505 Posts

    Re: Problem creating TreeMultiselectField to look up Page Link to this post

    Right, I have it working now:

    public static $many_many = array(
    'Box1Options' => 'SiteTree'
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();

    $fields->addFieldToTab( 'Root.Content.CopyBoxes', new TreeMultiselectField( 'Box1Options', 'Possible Links', 'SiteTree' ) );

    return $fields;
    }

    For some reason SilverStripe has issue with a $many_many that has a key with "Links" in it? :s

    Hope this helps someone.

    Mo

  • Aaron Brockhurst
    Avatar
    Community Member
    30 Posts

    Re: Problem creating TreeMultiselectField to look up Page Link to this post

    Hi Sean and Mo

    Thanks for your help with this.

    I've made these changes and still no luck.

    When I navigate to the page I am able to select multip options from the drop down and when I click publish it appears to save correctly. But when I navigate away and come back it hasn't saved the selections.

    On further investigation it will only save one link from one page and is not saving the others.

    Aaron

  • Mo
    Avatar
    Community Member
    505 Posts

    Re: Problem creating TreeMultiselectField to look up Page Link to this post

    Is your $many_many item still referencing Page? Or is it now SiteTree?

    When I tried to referencing Page my script failed.

    Mo

  • Aaron Brockhurst
    Avatar
    Community Member
    30 Posts

    Re: Problem creating TreeMultiselectField to look up Page Link to this post

    Hi Mo

    I restructured my object model as thought it might be that and it sort of followed the examples.

    I now have a SlimJimsContentPage where I've put

    public static $many_many = array(
        'SlimjimsLinkedPages' => 'SiteTree'
        );

    And this lets me select and save the linked pages for one page only.

    In the Page.php I've put

    public static $belongs_many_many = array(
          'SlimjimsBelongingPages' => 'SlimJimsContentPage'
       );

    However, it only saves the checked pages for one page. For all others I get a message saying it's saved them but it hasn't.

    Aaron

  • Mo
    Avatar
    Community Member
    505 Posts

    Re: Problem creating TreeMultiselectField to look up Page Link to this post

    Hi Aron,

    I just reread your original post, I am not sure why you are using $belongs_many_many?

    In order to get a list of pages (that is selected in the cms) to render on a page, all I do is:

    In mysite/code/Page.php

    class Page extends SiteTree {

    public static $db = array();

    public static $many_many = array(
       'LocationList'   => 'SiteTree'
    );

    function getCMSFields() {
       $fields = parent::getCMSFields();

       $fields->addFieldToTab( 'Root.Content.Main', new TreeMultiselectField( 'LocationList', 'Box Questions', 'SiteTree' ) );
          
       return $fields;
    }

    };

    class Page_Controller extends ContentController {
       
       public function init() {
          parent::init();
       }
       
    }

    Then in my themes/themename/templates/layout/Page.ss

    <ul>
       <% control LocationList %>
          <li><a href="$Link">$Title</a></li>
       <% end_control %>
    </ul>

    Is this not what you want to achieve?

    4370 Views
Page: 1 2
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.