3063 Posts in 864 Topics by 646 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1950 Views |
-
Inheriting value from a parent

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;
}
} -
Re: Inheriting value from a parent

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! -
Re: Inheriting value from a parent

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!
-
Re: Inheriting value from a parent

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
-
Re: Inheriting value from a parent

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.
| 1950 Views | ||
|
Page:
1
|
Go to Top |


