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.

Data Model Questions /

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

[SOLVED] Multiple News


Go to End


2 Posts   1112 Views

Avatar
Craftnet

Community Member, 58 Posts

21 October 2011 at 8:02am

Edited: 24/10/2011 5:56am

Hi,
I try to do multiple news consisting of News, "NewsPage" and the news "DiscographyNews".

I would like to be displayed by date.
At the moment I found such a thing

function LatestNews($num=10) {
    	return DataObject::get("ArticlePage", "", "Date DESC", "", $num);	
  }

Everything is good because it displays each note of the layout Articlepage (anywhere I would not put it)
When I try insert this code:

function LatestNews($num=10) {
    	return DataObject::get("DiscographyPage", "", "Date DESC", "", $num);	
  }     

displays all DiscographyPage

I would like to combine it into one to sort by date and display all at once on the home page

I tried this:

function LatestNews($num=10) {
    	return DataObject::get("ArticlePage", "", "Date DESC", "", $num);
        return DataObject::get("DiscographyPage", "", "Date DESC", "", $num);		
  }     

But then retrieves only the first "return".

It can be somehow unite?

I find that but this not sort by date

function LatestNews($num=15) {
$articles = DataObject::get("ArticlePage", "", "Date DESC", "", $num);
$disc = DataObject::get("DiscographyPage", "", "Date DESC", "", $num);

$articles->merge($disc);
return $articles->getRange(0, $num);
}

Struck-out text because: See post below

Sorry for my bad English

Avatar
Craftnet

Community Member, 58 Posts

24 October 2011 at 5:53am

I answer myself but it may be useful to someone.

I found a solution.
In function LatestNews need to insert such a thing

function LatestNews($num=15) {
	$sorcik = new DataObjectSet();
	$sorcik->merge(DataObject::get("ArticlePage", "", "Date DESC", "", $num));
	$sorcik->merge(DataObject::get("DiscographyPage", "", "Date DESC", "", $num));
	$sorcik->merge(DataObject::get("BiographyPage", "", "Date DESC", "", $num));
	$sorcik->sort('Date', 'DESC');
	return $sorcik->getRange(0, $num);
}

Now function LatestNews collects all the news of the code pages (in my case: ArticlePage, DiscographyPage and BiographyPage) and sorts them by date.

Sorry for my bad English