Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions

Problem creating TreeMultiselectField to look up Page


Go to End
Reply

14 Posts   4478 Views

Avatar
Aaron Brockhurst

14 March 2009 at 4:11am (Last edited: 14 March 2009 4:11am), Community Member, 30 Posts

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

Avatar
Sean

23 March 2009 at 9:48pm Forum Moderator, 921 Posts

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.

Avatar
Mo

23 March 2009 at 11:09pm (Last edited: 23 March 2009 11:10pm), Community Member, 509 Posts

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

Avatar
Mo

24 March 2009 at 5:50am Community Member, 509 Posts

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

Avatar
Aaron Brockhurst

25 March 2009 at 3:51am Community Member, 30 Posts

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

Avatar
Mo

26 March 2009 at 9:59am Community Member, 509 Posts

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

When I tried to referencing Page my script failed.

Mo

Avatar
Aaron Brockhurst

26 March 2009 at 10:07am Community Member, 30 Posts

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

Avatar
Mo

27 March 2009 at 10:28am Community Member, 509 Posts

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?

Go to Top