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.

General Questions

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

News from multiple holders within a section


Go to End
Reply

14 Posts   2391 Views

Avatar
headless_pnub

21 February 2009 at 2:47am Community Member, 17 Posts

Hi,

I'm trying to create a rather complex news system. My website has a number of sections, some of which have more than one news holders and what I want is for the root of that section to have all of is child news (across all of the holders within that section).
I hope this makes sense.

I have got an overall news, which shows everything and a individual news for that specific holder, but I've got stuck on this bit. If anyone has any clue, I'd greatly appreciate it.

Thanks.

Avatar
Carbon Crayon

21 February 2009 at 3:20am (Last edited: 21 February 2009 3:23am), Community Member, 598 Posts

Bit of a hack but it should work. There is probably a cleaner way of doing it.....

function LatestNewsInSection(){

$Holders = DataObject::get('ArticleHolder', 'ParentID= $this->ID');

if($Holders){
$i = 0
foreach($Holders as $Holder){

if($i == 0){
$Filter = 'ParentID =' . $Holder->ID;
$i=1;
}
else{
$Filter = $Filter . ' OR ParentID =' . $Holder->ID;
}

}
return DataObject::get('ArticlePage', '$Filter');
}
else{
return flase;
}
}

Avatar
headless_pnub

21 February 2009 at 4:43am Community Member, 17 Posts

Thanks for the quick reply.

I seem to get issues with the first line: $Holders = DataObject::get('ArticleHolder', 'ParentID= $this->ID');
it only works if I have get_one, but then it's obviously only getting 1 holder.

It looks like the code would do what I want if only I could get it to work :).
Thanks again.

Avatar
Carbon Crayon

21 February 2009 at 5:13am Community Member, 598 Posts

hmmm that is strange....what happens if you just have DataObject::get('ArticleHolder');

Perhaps the $this->ID needs curly braces: '{$this->ID}', or just separate it completely :

$ID = $this->ID;
DataObject::get('ArticleHolder', 'ParentID= $ID');

Avatar
headless_pnub

21 February 2009 at 6:06am Community Member, 17 Posts

OK, I'm getting closer.

using: $Holders = DataObject::get('ArticleHolder'); returns me 3 ArticleHolder's (which is correct across the whole site).
But when I use: $Holders = DataObject::get('ArticleHolder', 'ParentID =' . $this->ID); to get all the holders in the section, it only looks at the direct children.

I need to somehow combine the two.
Thanks for all your help so far.

Avatar
Carbon Crayon

21 February 2009 at 6:10am Community Member, 598 Posts

So your article holders are not direct children of your section? Hmm that makes it harder because we don't have a field in the Article Holder to identify which section it is in......

Any chance you could show me what your site structure looks like by class? something like:

PageTypeA
---ArticleHolder
---PageTypeB
--------ArticleHolder
PageTypeC
---PageTypeD
--------ArticleHodler

etc.

Avatar
headless_pnub

21 February 2009 at 6:33am Community Member, 17 Posts

Ahh yes, I see now. The code would work if I had two article holders inside the section rather than at lower levels.
My current structure is as follows:

PageLevel1-(a)
---ArticleHolder
---PageLevel2
PageLevel1-(b)
---PageLevel2
--------ArticleHolder
---PageLevel2
--------ArticleHolder

So on each PageLevel1 I'd like to show the content from the ArticleHolders in lower levels. On PageLevel1-(a) that easy because it's a direct child, but on PageLevel1-(b) I'd like to see the children from both ArticleHolders.

Hope that makes sense.

Avatar
Carbon Crayon

21 February 2009 at 6:37am Community Member, 598 Posts

Ok, I think your best bet for this would be to have a section tag in the ArticleHolder which you can set, say a the URLSegment of the top level page. That way in you getLatestArticles Function you can do:

DataObject::get('ArticleHolder', 'Section = $this->URLSegment');

Go to Top