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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Setting Extension Relation without Touching Module Core???


Go to End


4 Posts   867 Views

Avatar
TinnedTuna

Community Member, 3 Posts

5 March 2012 at 2:56pm

I've been learning silverstripe for a little while now but I've come across an issue that I hope someone can help me with. I've created a DataObject (let's call it PaperClip). I want to attach my PaperClip to a Page - sure - I form an extension using PaperClip and decorate SiteTree or Page with it. In my Page I specify "$has_one = array('PaperClip'=>'PaperClip')". Now, how do I specify the reverse relationship in my PaperClip object (Has One Page) without modifying the PaperClip DataObject or PaperClip Extension code?

The goal is to abstract my PaperClip so it can be attached to any object type, a Page, a Memo, an Envelope, a Dog.

Maybe something like: mysite/_config -> PaperClip::$has_one = array('Dog'=>'Dog'); Should I wish to attach my PaperClip to a dog.

Thanks.

Avatar
Willr

Forum Moderator, 5523 Posts

6 March 2012 at 6:27pm

Take a look at how Comments (https://github.com/silverstripe/silverstripe-comments/blob/master/code/Commenting.php#L46) or Fulltextsearchable works. Usually you can have an extension that you apply to the page, and in that extension you add the new relationship.

Avatar
TinnedTuna

Community Member, 3 Posts

6 March 2012 at 11:00pm

Hey Will, thanks for that pointer. I might adopt your idea of providing a helper class with a module as I wasn't sure were to put a reference to the directory name of the module.

Back to the prob at hand: I'm not really having any trouble with the Parent->Child relationship since that is taken care of inherently when you use DataObject::addExentsion(....); What I'm not sure about is the Child->Parent relationship. Taking your code as an example, I see that you reference the DataObject you have attached Comments to by stating $has_one = array('Parent'=>'DataObject'); - https://github.com/silverstripe/silverstripe-comments/blob/master/code/dataobjects/Comment.php#L21

That's a good solution but what if I (as the coder) have no knowledge of how the end-user wants to use my PaperClip DataObject. They may wish to attach it to a page using many_many, has_one, etc. I'm still left wondering how to change the relationship in my PaperClipExtension (extraStatics method) and my PaperClip Data Object from outside the modules code.

Avatar
TinnedTuna

Community Member, 3 Posts

7 March 2012 at 12:34am

I've found a solution to this for anyone who is interested.

Turns out you can simply override the static properties of both classes. Crudely implemented with:

In PaperClip_DataObject.php I have:

public static function addRelationship($type, $accessor, $className) {
	$relationship = self::$$type;
	$relationship[$accessor] = $className;
	self::$$type = $relationship;
}

For some reason you can't state:

self::$$type[$accessor] = $className;

In my mysite/_config.php I have:

PaperClip_DataObject::addRelationship('has_one', 'Page' ,'Page');

Need to do add a method to the PaperClip_Extension to the same effect in order to override the relationship created by the extraStatics method.