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.

Data Model Questions /

Return a subset of children elements of a DataObjectSet


Reply


3 Posts   1314 Views

Avatar
Matthew

Community Member, 5 Posts

12 June 2009 at 6:58am

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.

Avatar
Hamish

Community Member, 712 Posts

12 June 2009 at 9:08am

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.

Avatar
Matthew

Community Member, 5 Posts

12 June 2009 at 11:53pm

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.