Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Sorting a DataObjectSet filled with ArrayData

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

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

Page: 1
Go to End
Author Topic: 2192 Views
  • dio5
    Avatar
    Community Member
    501 Posts

    Sorting a DataObjectSet filled with ArrayData Link to this post

    I'd like to know how I can sort a DataObjectSet that I've filled with ArrayData. I know I can sort it using the sort() method but I've tried different ways and never got a result.

    On my Page_Controller I have roughly this function:

    function()
    {
    $provinces = DataObject::get('Province'
    $doSet = new DataObjectSet();
             
    foreach($provinces as $province)
    {
       $amount = $province->Tips()->Count();
       $provinceTitle = $province->Title;
    $provinceID = $province->ID;
       $record = array('ProvId' => $provinceID, 'ProvTitle' => $provinceTitle, 'ProvTotal'=> $amount);
       $Ad = new ArrayData($record);
       $doSet->push($Ad)
    }

    return $doSet;
    }

    The reason it's on Page_Controller is that I want to call it on different types of pages...

    I tried using
    $provinces->groupBy(Title) as $province
    too but that gave an error.

    I would like to sort the data by ProvTitle, any hints on how to obtain this?
    I was thinking of

    $newSet = $doSet->sort(?????????)

    But I'm not sure of how to write the argument to pass..

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Sorting a DataObjectSet filled with ArrayData Link to this post

    I have solved it using some regular php, array_multisort, etc.. but seems more like a hack than the proper way to do it, so if someone knows the SS-way to do it, let me know

    Other question in the same field: how to do this with a restriction. Suppose I want to count all the items in a category but only those items where for example 'Viewers = "Anyone" ..?

    I know a lot of questions... feels like learning a new language here

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Sorting a DataObjectSet filled with ArrayData Link to this post

    your answer is basically: custom getters and better usage of templates.

    is there any reason for aliasing $province->ID to ProvId? you can use the original $provinces DataObjectSet in a template like this (with a function Provinces() on your Page_Controller)

    [html]
    <% control Provinces %>
    $Tips.Count
    $ID
    $Title
    $FilteredTips.Count
    <% end_control %>
    [/html]

    now, if you want to count e.g. all tips with "Viewers=Anyone"-filtering, and Province has_many Tips, you would just write a custom getter FilteredTips() on Province.

    class Province extends DataObject {
    $has_many = array('Tips'=>'Tip');

    function FilteredTips() {
    // alternatively you can use DataObject::get, but this is the best practice for has_many
    return $this->getComponents('Tips', "Viewers='Anyone'");
    }

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Sorting a DataObjectSet filled with ArrayData Link to this post

    ?! why wasn't this clear to me before...?

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Sorting a DataObjectSet filled with ArrayData Link to this post

    Obviously the getComponents approach is not working for many_many relations, any clues on this?

    Thanks for your help already!

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Sorting a DataObjectSet filled with ArrayData Link to this post

    Ok, found it in the 'all classes' page.
    for many_many relationships one should use getManyManyComponents()

    It's not completely clear to me how this is all working together and why DataObject::get doesn't do the trick, but it works...

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Sorting a DataObjectSet filled with ArrayData Link to this post

    DataObject::get works as well, but you have to specificy the id (or even manually join tables), which you don't have with the component-getters. the resulting object from component-getters is also a "special" DataObjectSet-subclass containing additional information on the queried relation (ComponentSet).

    2192 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.