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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

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

Getting first image thumbnail from a has_many

Go to End

2 Posts   1214 Views

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?


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