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.

Data Model Questions

Sorting and grouping many_many relationships


Reply

5 Posts   2618 Views

Avatar
D&B

16 December 2009 at 9:43am Community Member, 4 Posts

Hi,

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

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

17 December 2009 at 11:32pm (Last edited: 17 December 2009 11:34pm), Community Member, 21 Posts

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

4 May 2010 at 10:55am Community Member, 5 Posts

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[/url]) 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
Pigeon

5 May 2010 at 11:19am Community Member, 243 Posts

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

7 May 2010 at 5:29am Community Member, 5 Posts

This did the trick. Thank you so much!