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

Combining two lists of DataObjects, returning them as an iterable for templates


Go to End


4 Posts   1256 Views

Avatar
Neomang

Community Member, 9 Posts

31 March 2016 at 8:38am

I'm currently struggling with this issue. I have two sets of very similar DataObjects (share standard Title, Slug, Date, etc.). For the page they will be showing up on, I need to pull in the most current 2 items, regardless of which type they are. I've tried a couple different methods with little success. Even in cases where I have had success, the returned object did not work as intended in the template file.

Any suggestions would be appreciated.

Avatar
Babalou

Community Member, 21 Posts

1 April 2016 at 2:09am

It's not very clear what you are trying to achieve. Some code would help.

Avatar
Neomang

Community Member, 9 Posts

1 April 2016 at 5:22am

Edited: 01/04/2016 6:50am

Sorry, should have included some code from the start. Here's a basic idea of what I'm working on:

$numPosts = 0;
$eduOffset = 0;
$flexOffset = 0;

$posts = array();

while ($numPosts < $limit){
	
	if($this->EducationPost()->exists()){
		$eduStream = $this->EducationPost()->sort('Created', 'desc')->offsetGet($eduOffset);
	}
	else
	{
		$eduStream = null;
	}
	if($this->FlexPost()->exists()){
		$flexStream = $this->FlexPost()->sort('Created', 'desc')->offsetGet($flexOffset);
	}
	else{
		$flexStream = null;
	}

	if((is_null($flexStream) && !is_null($eduStream)) || $eduStream->column('Created') > $flexStream->column('Created')){
		array_push($posts, $eduStream);
		$eduOffset++;
		$numPosts++;
	}
//Further checks like the one above

return $posts;

Essentially, I want to look at all the EducationPosts and FlexPosts associated with the page, then take the most recent $limit number of posts, type agnostic, and return them as an iterable list that I can loop through on the page.

The problems I'm running into are twofold. This implementation fails when I try to check which type of post is more recent (which I could mitigate by changing the way the checks work, but it feels like it'd be overcomplicating the issue) due to the ->column() call. Additionally, when testing just to make sure the returned object is iterable, the template does not iterate through the list of returned objects. the code for this snippet is as follows:

<ul>
<% loop ListPosts(3) %>
	<li>$Title</li>
<% end_loop %>
</ul>

Avatar
camfindlay

Forum Moderator, 267 Posts

5 April 2016 at 10:24am

I think you need to return the objects as an ArrayList() object in order for the template to be able to correctly loop over them. See https://docs.silverstripe.org/en/3.1/developer_guides/model/lists/#arraylist and http://api.silverstripe.org/3.1/class-ArrayList.html