Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Sorting Articles

Our old forums are still available as a read-only archive.

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

Page: 1 2
Go to End
Author Topic: 5063 Views
  • saimo
    Avatar
    Community Member
    67 Posts

    Sorting Articles Link to this post

    I want to sort the ArticlePages within an ArticleHolder, but I cannot figure out how exactly.
    I've largely followed the first and the second tutorial, and I have also read a lot of information in the documentation, but all I find is http://doc.silverstripe.com/doku.php?id=datamodel#using_dataobjectrequest but I don't understand how to use it.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Sorting Articles Link to this post

    How did you want to sort them. Normally say if you want the latest NewsArticles you would do something like

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

    Notice the 3 parameter in the DataObject call? This is the sort by field. So in our example above it would sort by the Date Field and DESC.

  • saimo
    Avatar
    Community Member
    67 Posts

    Re: Sorting Articles Link to this post

    That seems to work pretty well, thanks.
    But then I got a second question:
    How do I make the number of items to be displayed configureable from the CMS? Or, as I'm not shure about that kind of functionality, lets turn it into a more generic question:
    How do I easily access DB fields in php code?

    EDIT:
    I placed the code in the ArticleHolder class, Is that where it should be placed? It seems to work from ArticleHolder_Controller too.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Sorting Articles Link to this post

    It depends where you want to call that function. It should always be in the controller section but for example if you had that in ArticleHolder you would only be able to call that function on the ArticleHolder page type so for example not blog entries or other pages

  • saimo
    Avatar
    Community Member
    67 Posts

    Re: Sorting Articles Link to this post

    Ok, I'll move it to the controller then, what kinds of things should be put into classes inheriting from Page?

  • saimo
    Avatar
    Community Member
    67 Posts

    Re: Sorting Articles Link to this post

    I've found another problem:
    If I've got two ArticleHolder pages on my site, LatestNews will show Articles from all the ArticleHolder's.
    This is how I think it could work but it doesn't:
    function LatestNews($limit = "10") {
    $data = $this->Children();
    //return DataObject::get("ArticlePage", "", "Created DESC", "", $limit);
    return $data->sort("Created DESC");
    }
    This is based on what I've seen in the documentation, but how should it be done?
    Since I'm just scratching the surface of silverstripe here, this doesn't seem very logical at all, not really intuitive, at least not to me.

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Sorting Articles Link to this post

    You'd probably end up with doing something like this:

    function LatestArticles($limit)
    {
    return DataObject::get("ArticlePage", "ParentID = {$this->ID}", "Created DESC", "", $limit);
    }

    of course that's only working when you call it on ArticleHolder.

    If you do a sort, I think you have to split the field and the sort order:

    like

    $data = $this->Children();
    $data->sort("Created", "DESC");

  • saimo
    Avatar
    Community Member
    67 Posts

    Re: Sorting Articles Link to this post

    That worked great, thanks!
    It's not so easy know how to use the parameters, though I had guessed it had to do with the second parameter.

    $data = $this->Children();
    $data->sort("Created", "DESC");
    didn't work however, and I don't know why.

    Thanks also for indirectly aswering one of my previous questions, how to access fields from php code.

    5063 Views
Page: 1 2
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.