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.

Data Model Questions /

Inheriting value from a parent


Reply


5 Posts   2388 Views

Avatar
ajxtee

Community Member, 8 Posts

10 December 2009 at 9:31am

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;

   }
   
}

Avatar
Willr

Forum Moderator, 5513 Posts

10 December 2009 at 11:56am

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!

Avatar
ajxtee

Community Member, 8 Posts

10 December 2009 at 12:00pm

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!

Avatar
ajxtee

Community Member, 8 Posts

16 December 2009 at 1:16am

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

Avatar
SamSmooth

Community Member, 18 Posts

4 November 2010 at 8:00am

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.