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.

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

get one Element from has_many


Go to End


3 Posts   1891 Views

Avatar
oleze

Community Member, 65 Posts

10 October 2010 at 12:56am

I have the following structure:

|-HolderPage
|---Page 1
|---Page 2

where Page 1 & 2 are children of the HolderPage. In my case, my Pages have a "has_many" relation to ImageResources (Images with Title and Caption). Now I'd like to automatically generate a list of the children within my HolderPage (that works really well) and add one (the first) of the ImageResources as a thumbnail to the list-entries on my HolderPage. I tried

function getOneHiglightImage() {
   	return $this->HighlightImages("", "SortOrder ASC", null, 1);
}

within Page.php. For the template of the HolderPage I'm using:

function getChildren() {
  		return DataObject::get("Page", "ParentID = $this->ID", "Title ASC", "", "" );
	}

and

<ul id="List">
    			<% control getChildren %>
      				<li>
  					   	<div class="Photo">
  					   	        $getOneHiglightImage.HighlightAttachment  					   		
  					   	</div>
   					   	<div class="Title">
   					   		<% control Type %>
   					   			<a href="$Link" title="$Title">$Title</a> 
   					   		<% end_control %>
   					   		<a href="$Link" title="$Title">"$Title"</a></div>
   						<div class="Summary">$Content.FirstParagraph</div>
    					<div class="ReadOn clearfix">
    						<a href="$Link" title="$Title"><% _t('READMORE','Read more') %> ...</a>
    					</div>
    				</li>
    			<% end_control %>
  			</ul>

HighlightAttachment is where the Image is stored. But that doesn't work for me. Can someone help me?

Avatar
(deleted)

Community Member, 473 Posts

10 October 2010 at 12:03pm

Why can't you just use $HighlightImages.First?

Avatar
oleze

Community Member, 65 Posts

11 October 2010 at 12:37am

Because I didn't know that it works that easy ;). Thank you.