Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Return a subset of children elements of a DataObjectSet

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

Page: 1
Go to End
Author Topic: 1248 Views
  • Matthew
    Avatar
    Community Member
    5 Posts

    Return a subset of children elements of a DataObjectSet Link to this post

    Here is my situation:
    I have one class StadtBild extends Dataobject, which is in an has_one / has_many with Stadt (ie. each Stadt has many Stadtbilder)

    I would like to put a summary of these on the front page, which is not in the same hierarchy as Stadt. I also have many instances of Stadt.

    on the front page, i have the following function in the controller:

    function summarize(){
    $pages = DataObject::get('Stadt', "", 'LastEdited DESC', null, 5);

    foreach($pages as $page){

       $page->StadtBilder = DataObject::get("StadtBild", "StadtID = $page->ID", "Name DESC", "", 5);
    }

    return $pages;

    }

    Basically, the DataObject subset $page->StadtBilder refuses to be overwritten. I've tried the obvious methods (add, remove) listed here(http://api.silverstripe.com/default/DataObjectSet.html) as well as a few other ways of doing it. For reference, the template files looks like this:

    <% control summarize %>
       <div class="summaryitem"><a href="$Link">$Title</a>      
          <ul class="summarylist">
             <% control StadtBilder %>
                <li title="$Name"><a href="$Link" target="_blank">$Attachment</a><br /> $Description
             <% end_control %>
          </ul>
       </div>
    <% end_control %>

    The result of the current code is that the StadtBilder are not reduced to the first 5.

    I've had really bad luck getting responses here, if my questions isn't helpful, could someone please help me to ask a more answerable question? I'm keen to learn, even after day 2 of this problem.

    Thanks in advance.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Return a subset of children elements of a DataObjectSet Link to this post

    I suspect what is happening (and if someone who knows better can confirm this, please do) is that in the template, the method StadtBilder is evaluated for each Stadt in the control loop, rather than picking up the value you have assigned it.

    I would modify this so that you have a new method that is called on each Stadt instead that returns the summary info, ie:

    class Stadt extends Page {

       // your code

       function BilderSummary() {
          return DataObject::get("StadtBild", "StadtID = $this->ID", "Name DESC", "", 5);
       }

    }

    Now modify you master summarize method so that:

       function summarize(){
          return DataObject::get('Stadt', "", 'LastEdited DESC', null, 5);
       }

    No, you can do this in your template:

    <% control summarize %>
    <div class="summaryitem"><a href="$Link">$Title</a>
       <ul class="summarylist">
          <% control BilderSummary %>
          <li title="$Name"><a href="$Link" target="_blank">$Attachment</a><br /> $Description
          <% end_control %>
       </ul>
    </div>
    <% end_control %>

    So, summarize returns you recently edited Stadt pages, and the BilderStummary method returns the Bilder summary info for each page, rather than trying to overwrite the value from the first method.

  • Matthew
    Avatar
    Community Member
    5 Posts

    Re: Return a subset of children elements of a DataObjectSet Link to this post

    That's it! I never thought to put the summarize function into the Page class, it makes more sense, and you can use the $this thingy.

    As soon as I saw it, i was like, of courrrrrrrrrrrrrrse! Thanks a lot, Big help.

    1248 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.