Jump to:

22998 Posts in 11620 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » Setting Extension Relation without Touching Module Core???

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

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

Page: 1
Go to End
Author Topic: 364 Views
  • TinnedTuna
    Avatar
    Community Member
    3 Posts

    Setting Extension Relation without Touching Module Core??? Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Setting Extension Relation without Touching Module Core??? Link to this post

    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.

  • TinnedTuna
    Avatar
    Community Member
    3 Posts

    Re: Setting Extension Relation without Touching Module Core??? Link to this post

    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.

  • TinnedTuna
    Avatar
    Community Member
    3 Posts

    Re: Setting Extension Relation without Touching Module Core??? Link to this post

    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.

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