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

News from multiple holders within a section


Go to End


14 Posts   3735 Views

Avatar
headless_pnub

Community Member, 17 Posts

21 February 2009 at 2:47am

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

Community Member, 598 Posts

21 February 2009 at 3:20am

Edited: 21/02/2009 3:23am

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

Community Member, 17 Posts

21 February 2009 at 4:43am

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

Community Member, 598 Posts

21 February 2009 at 5:13am

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

Community Member, 17 Posts

21 February 2009 at 6:06am

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

Community Member, 598 Posts

21 February 2009 at 6:10am

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

Community Member, 17 Posts

21 February 2009 at 6:33am

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

Community Member, 598 Posts

21 February 2009 at 6:37am

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