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

Sorting and grouping many_many relationships


Go to End


5 Posts   4043 Views

Avatar
D&B

Community Member, 4 Posts

16 December 2009 at 9:43am

Hi,

I've worked through the many_many example provided in the documentation (http://doc.silverstripe.org/doku.php?id=recipes:many_many-example).

I've tried and tried, but I can't work out how to group/sort the articles on the front end.

For example, say I have:

- Article 1 (category = catA)
- Article 2 (category = catB)
- Article 3 (category = catA)
- Article 4 (category = catA, catB)

I want the articles to display like this on the front end:

<h1>CatA</h1>
Article 1, Article 3, Article 4

<h1>CatB</h1>
Article 2, Article 4

Any ideas how I can do this? Keep in mind that I don't know what the categories will be, they are defined by the content editor.

Thank you.

Avatar
wee-man

Community Member, 21 Posts

17 December 2009 at 11:32pm

Edited: 17/12/2009 11:34pm

Hi,

you can do this with a method in your Article-controller.

...
function ArticlesByCat($cat) {
//get all articles with category $cat
$articles = DataObject::get("Article",
					"category=".$cat,
					"",
					"",
					"");
//return them
return $articles;
}
...

Then in your Template use

...
<% control ArticlesByCat('catA') %>
$Article_Description...
<% end_control %>
...

The template is calling the method ArticlesByCat and iterates over the returned Articles.

Sorting etc. is done by modifying DataObject::get....

Greetings
Michael

Avatar
earleysc

Community Member, 5 Posts

4 May 2010 at 10:55am

I am trying to do something very similar for the news site I am building but haven't been able to get a successful result when following along with this or other similar examples.

I have a many to many relationship between the below two .php files.
Articles can belong to multiple Categories.
A Category can include multiple Articles.

NewsArticlePage.php

class NewsArticlePage extends Page {

static $many_many = array(
      'Categories' => 'Category'
   );

...

Category.php

class Category extends DataObject {
...
static $belongs_many_many = array(
      'Articles' => 'NewsArticlePage'
   );

I have one sample article built now, an instance of the NewsArticlePage page type, and using a checkbox form I added to the CMS (following the [url-http://doc.silverstripe.org/recipes:many_many-example/]many_many recipe) I assigned the article to a category with the ID of "1" and the title of "TopNews".

I want to display on my home page the $Title and $Content.FirstParagraph of whatever article is currently assigned to the "TopNews" category, which, as I said, right now is my sample article.

The home page is of the "Page" page type and I'm planning to call up the "TopNews" article data via a Control statement within an Include.

I've tried many approaches but keep running into a wall when it comes to retrieving the "TopNews" article's data. My understanding is that I need to add a function within my NewsArticlePage controller where I do some variation of "return DataObject:get..."

If someone could point me in the right direction I'd greatly appreciate it. This one issue has really been holding me back.

Thanks,
Steve

Avatar
dhensby

Community Member, 253 Posts

5 May 2010 at 11:19am

I'm very sleepy, but this is one way you can fetch the latest article of from the TopNews category.

Either, get the Top news category out and then get the latest news:
HomePage controller:

function getLatestTopNews() {
$topNews = DataObject::get_one('Category',"Title = 'TopNews'");
return $topNews->Articles(null,'ID DESC',null,1);
}

then, in the template:
<% control LatestTopNews %>
$Title
$Content.FirstParagraph
<% end_control %>

Avatar
earleysc

Community Member, 5 Posts

7 May 2010 at 5:29am

This did the trick. Thank you so much!