Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Inheriting value from a parent

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

Page: 1
Go to End
Author Topic: 2325 Views
  • ajxtee
    Avatar
    Community Member
    8 Posts

    Inheriting value from a parent Link to this post

    Hi Guys,
    I'm really new to Silverstripe but loving it. I just have one really important question for the website I'm designing. I've got a field on all of my page types called GalleryID and what I'd like to happen is for child pages when created to take the value of their parent page's GalleryID until they're given a value of their own. Does that make sense? Any help would be much appreciated!!

    The very start of my page.php is:

    class Page extends SiteTree {
       
       public static $db = array(
          'Sidebar' => 'HTMLText',
          'GalleryID' => 'Int'
       );
       
       public static $has_one = array(
       );
       
       function getCMSFields() {
            $fields = parent::getCMSFields();

            $fields->addFieldToTab('Root.Content.Sidebar', new HtmlEditorField('Sidebar', ''));
              $fields->addFieldToTab('Root.Content.Main', new NumericField('GalleryID','Gallery ID'), 'Content');

            return $fields;

       }
       
    }

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Inheriting value from a parent Link to this post

    well if you want to traverse up the tree you could use a while loop - something like

    $galleryID = $this-> GalleryID; // this object

    if(!$galleryID) {
    $parent = $this->Parent();
    if(!($galleryID = $parent->GalleryID)) {
    while($parent->Parent() && $parent->Parent()->exists() && !$galleryID) {
    $galleryID = $parent->GalleryID;
    }
    }
    }
    // $galleryID = yay!

  • ajxtee
    Avatar
    Community Member
    8 Posts

    Re: Inheriting value from a parent Link to this post

    Cool. Thanks! I'll try that out. Where exactly do I position that portion of code? I'm very new to all of this and still a little fuzzy on how it all fits together!

  • ajxtee
    Avatar
    Community Member
    8 Posts

    Re: Inheriting value from a parent Link to this post

    Is anyone able to give me an answer on this? This is the last thing I have to do on this site so it would be great to know!

    Thanks

  • SamSmooth
    Avatar
    Community Member
    18 Posts

    Re: Inheriting value from a parent Link to this post

    How can I get the parent's gallery (has_many)?

    static $db = array(
    'HeaderType' => "Enum('Image,Flash','Image')",
    );

    static $has_many = array(
    'HeaderImages' => 'ProductHolderHolderHeaderImage',
    );

    $parent = $this->Parent();
    $this->HeaderType = $parent->HeaderType;
    $this->HeaderImages = $parent->HeaderImages;

    $parent->HeaderImages will not work (is empty) in my case.

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