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.

Archive /

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

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

Newbie: Item with exclusive one-to-many Items. How?


Go to End


7 Posts   2823 Views

Avatar
bummzack

Community Member, 904 Posts

22 February 2008 at 7:48am

Edited: 02/03/2008 9:43am

Hi all.
I'm new to SilverStripe and would like to thank the Developers for such a great piece of Software.

What i'm currently trying to do is the following: Create a Product Item and allow the Administrator to add 0..* Web-Links to it. The Web-Link item consists of a Title and a URL Field. I don't want to add the Links in the Content-Box as Text, because they will appear in a separate Section on the Site.

I found the almost perfect solution in the tutorials about one-to-many relations. The only thing that should be different, is that Web-Links should be strictly bound to one Product Item and not be selectable (nor visible) on other Product Item Pages.

I hope you guys understand what i mean, i'm not yet familiar with the SilverStripe terminology :)
Any input, hints etc. are very welcome.

Avatar
bummzack

Community Member, 904 Posts

23 February 2008 at 1:35am

Edited: 23/02/2008 1:35am

Okey, i was able to resolve this myself. Although it needed quite some digging inside the Docs, and looking at the sources...

Well, i guess that's how you learn the most isn't it?

I solved the issue by extending the HasManyComplexTableFields Class. Now i'm able to have 0..* items that are exclusively bound to one "Parent" item.
Here's the new Class for anybody that might be interested in this. Not sure if that's really the Way to do this, but it works.

<?php
/*
 * Models a ComplexTableField that will be exclusive to the parent
 */
class HasManyComplexTableFieldOnce extends HasManyComplexTableField {
	// set relationAutoSetting to true. We want the item to belong to just one parent item
	protected $relationAutoSetting = true;

	function __construct(
		$controller,
		$name,
		$sourceClass,
		$fieldList,
		$detailFormFields = null,
		$sourceFilter = "",
		$sourceSort = "",
		$sourceJoin = ""
	)
	{
		if($sourceFilter == '')
			$sourceFilter = $controller->class . 'ID = ' . $controller->ID;

		parent::__construct
		(
			$controller,
			$name,
			$sourceClass,
			$fieldList,
			$detailFormFields,
			$sourceFilter,
			$sourceSort,
			$sourceJoin
		);
		// set markable to false, we won't need it
		$this->Markable = false;
		$this->joinField = $this->getParentIdName
		(
			$this->controller->ClassName,
			$this->sourceClass
		);
	}

	// override the saveInto method. We don't need it for our purpose
	function saveInto( DataObject $record ) {}
}
?>

Avatar
bummzack

Community Member, 904 Posts

23 February 2008 at 1:40am

Edited: 23/02/2008 1:42am

I attached a Screenshot for you to see how it looks like in the CMS. The links are added to just one item and not available on others. No checkboxes needed.

Edit:
That didn't seem to work. Oh well, got to get used to this forum as well :)
Here's another try:

Avatar
newjamie

Community Member, 7 Posts

23 February 2008 at 12:34pm

Would you be able to post your code for this please?

I've been having massive problems creating a one-to-many link editor for one of my pages.

Thanks!

Avatar
bummzack

Community Member, 904 Posts

23 February 2008 at 1:33pm

Hi newjamie

Have you tried to do it like described in the tutorials?
Have a look here.

That actually worked fine for me. I just needed a one to many relation that hides the items used by other pages. Thus i created a special class to fit my needs. You can see the code for this class in my previous post. Implementation is simple: Just replace HasManyComplexTableField with HasManyComplexTableFieldOnce (look at the Tutorial code).

If you're good with the "standard" implementation, you won't need my code at all.
Good luck

Avatar
newjamie

Community Member, 7 Posts

23 February 2008 at 9:59pm

Ah ok, no worries - I misunderstood your post.

What I'm having trouble with is a one-to-many that uses a TreeDropdownField to populate a link from the SiteTree. I cannot get this working in the popup editor for some reason and get a security error.

Any ideas?

Thanks

Avatar
zyko

Community Member, 66 Posts

7 May 2008 at 4:54am

Hi Banal,

WOWSERS, THX a lot for this excellent piece of code.
Saved me some time, because i've got exactly the same problem :-)

THX
Helmut