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.

General Questions /

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

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

Holder Pages showing limited number of Child Page Elements

Go to End

11 Posts   5865 Views


Community Member, 39 Posts

29 January 2011 at 4:08pm

Thanks MonkeyBen and Webbower. The issue is now *SOLVED* thanks to your input.

For someone reading this in the future bleary eyed and frustrated at 2am in the morning :-) here is the scoop when working with a has many relationship in a classic SilverStripe PageHolder / Page setup. In short Webbower was spot on. The method call to the get the children of the ImageGalleryPage had to be in the Page subClass block and not in the Page_Controller block. As soon as I moved the method up to the Page subClass block the call using $this recognized its current scope and was able to get it associated has many relationship.

class ImageGalleryPage extends Page {

    static $db = array(
        'GalleryTitle' => 'Text',
        'GalleryExcerpt'=> 'Text'
    static $has_many = array (
        'ImageGalleryPageImages' => 'ImageGalleryPageImage'

    function ImageGalleryShowNum($num = 6) { 
        $results = $this->getComponents('ImageGalleryPageImages', '', 'SortOrder DESC', '', $num);
        return $results;

So if you are looking to iterate over you Pages in a holder relationship and those Pages have a has many relationship just follow this style to get at the data you need.


Community Member, 10 Posts

30 January 2011 at 7:06am

Alternatively, if you MUST have it in your Controller subclass the following should work, but it must be in the Controller subclass that is paired with the Page subclass:

function ImageGalleryShowNum($num = 6) { 
    $results = $this->data()->getComponents('ImageGalleryPageImages', '', 'SortOrder DESC', '', $num); 
    return $results; 

Calling $this->data() in your Controller subclass will return the paired Page subclass instance.


Community Member, 41 Posts

7 May 2011 at 5:07am

Thank you very much edk. You saved my night :) For last two hours I was trying to solve this issue. I tried everything except putting the function in the subclass. Thank you for great tip.

Go to Top