Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Sorting and grouping many_many relationships

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

Page: 1
Go to End
Author Topic: 2489 Views
  • D&B
    Avatar
    Community Member
    4 Posts

    Sorting and grouping many_many relationships Link to this post

    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.

  • wee-man
    Avatar
    Community Member
    21 Posts

    Re: Sorting and grouping many_many relationships Link to this post

    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

  • earleysc
    Avatar
    Community Member
    5 Posts

    Re: Sorting and grouping many_many relationships Link to this post

    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

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Sorting and grouping many_many relationships Link to this post

    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 %>

  • earleysc
    Avatar
    Community Member
    5 Posts

    Re: Sorting and grouping many_many relationships Link to this post

    This did the trick. Thank you so much!

    2489 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.