Jump to:

23007 Posts in 11866 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » get one Element from has_many

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

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

Page: 1
Go to End
Author Topic: 1187 Views
  • oleze
    Avatar
    Community Member
    64 Posts

    get one Element from has_many Link to this post

    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?

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: get one Element from has_many Link to this post

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

  • oleze
    Avatar
    Community Member
    64 Posts

    Re: get one Element from has_many Link to this post

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

    1187 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.