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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Getting first image thumbnail from a has_many


Go to End


2 Posts   1847 Views

Avatar
Matty Balaam

Community Member, 74 Posts

13 October 2013 at 1:44am

I have a gridfield in SectionPage managing a has_many dataobject 'Sections' => 'Section', which in turn has_many 'Gallery' => 'GalleryItem' which has_one image.

How can I get the Gridfield to return a thumbnail of the first GalleryItem?

I have tried many variations in Section.php, but feel this is the closest I have got:

public function getThumbnail() {
return $this->Gallery()->First()->Photo()->CMSThumbnail();
}

This gives me a 'Call to a member function Photo() on a non-object in...' error.

Can anyone give me some help with this problem?

Avatar
zenmonkey

Community Member, 545 Posts

14 October 2013 at 6:41pm

Make sure $this->Gallery()->First() is returning an object and not just a VIewableData array. I know I've run into this in the past

It may alos work to create a getThumbnial on GalleryItem and set it as virtual property