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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Problem creating TreeMultiselectField to look up Page


Go to End


14 Posts   7889 Views

Avatar
Aaron Brockhurst

Community Member, 30 Posts

14 March 2009 at 4:11am

Edited: 14/03/2009 4:11am

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

Forum Moderator, 922 Posts

23 March 2009 at 9:48pm

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

Community Member, 541 Posts

23 March 2009 at 11:09pm

Edited: 23/03/2009 11:10pm

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

Community Member, 541 Posts

24 March 2009 at 5:50am

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

Community Member, 30 Posts

25 March 2009 at 3:51am

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

Community Member, 541 Posts

26 March 2009 at 9:59am

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

Community Member, 30 Posts

26 March 2009 at 10:07am

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

Community Member, 541 Posts

27 March 2009 at 10:28am

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