Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Newbie: Item with exclusive one-to-many Items. How?

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

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

Page: 1
Go to End
Author Topic: 1986 Views
  • banal
    Avatar
    Community Member
    901 Posts

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

    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.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Newbie: Item with exclusive one-to-many Items. How? Link to this post

    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 ) {}
    }
    ?>

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Newbie: Item with exclusive one-to-many Items. How? Link to this post

    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:

  • newjamie
    Avatar
    Community Member
    7 Posts

    Re: Newbie: Item with exclusive one-to-many Items. How? Link to this post

    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!

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Newbie: Item with exclusive one-to-many Items. How? Link to this post

    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

  • newjamie
    Avatar
    Community Member
    7 Posts

    Re: Newbie: Item with exclusive one-to-many Items. How? Link to this post

    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

  • zyko
    Avatar
    Community Member
    66 Posts

    Re: Newbie: Item with exclusive one-to-many Items. How? Link to this post

    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

    1986 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.