Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » [SOLVED] Grouping Output

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1527 Views
  • zenmonkey
    Avatar
    Community Member
    528 Posts

    [SOLVED] Grouping Output Link to this post

    I have a list of Retailers and Distributors I want to output in the template sorted by continent, country and category

    Below is the Data Object

    class ProductSeller extends DataObject
    {
       static $db = array (
          'SellerName' => 'Text',
          'SellerDescription' => 'Text',
          'SellerCategory' => "Enum('Retail, Distributor, Web')",
          'SellerLocation' => 'Text',
          'SellerContinent' => "Enum('NorthAmerica, SouthAmerica, Europe, Asia, Australia')",
          'SellerURL' => 'Text',
          'SellerPhone' => 'Text'
       );
       
       static $has_one = array (
          'WhereToBuyPage' => 'WhereToBuyPage'
       );
       
       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('SellerName'),
             new TextareaField('SellerDescription'),
             new DropdownField('SellerCategory','SellerCategory', singleton('ProductSeller')->dbObject('SellerCategory')->enumValues()),
             new CountryDropdownField('SellerLocation'),
             new DropdownField('SellerContinent','Continent', singleton('ProductSeller')->dbObject('SellerContinent')->enumValues()),
             new TextField('SellerURL'),
             new TextField('SellerPhone')
          );
       }
       
    }

    In the template I can group the SellerContinent and SellerCategory by if statements, however is there an easy way to group the countries without manually making an if statements for every country.

    I wrote a function thats sorts the Data by country so worse comes to worse I can use the country as a class and jQuery the whole thing and just give the JavaScript-less people a sorted list.

    function sortByCountry(){
          $countrys = DataObject::get("ProductSeller","", "SellerLocation");
          return $countrys;
       }

    Thanks

  • UncleCheese
    Avatar
    4085 Posts

    Re: [SOLVED] Grouping Output Link to this post

    Try this:

    WhereToByPage.php

    function Countries()
    {
    $countries = new DataObjectSet();
    $results = DB::query("SELECT DISTINCT SellerLocation FROM ProductSeller ORDER BY SellerLocation ASC");
    if($results) {
    foreach($results as $result) {
    $country = $result['SellerLocation'];
    $countries->push(new ArrayData(array(
    'Country' => $country,
    'Sellers' => DataObject::get("ProductSeller","SellerLocation = '$country'")
    )));
    }
    return $countries;
    }

    return false;
    }

    WhereToBuyPage.ss

    <% control Countries %>
    <h3>$Country</h3>
    <ul>
    <% control Sellers %>
    <li>$SellerName</li>
    <% end_control %>
    </ul>
    <% end_control %>

    Can't promise it's free of syntax errors, but that's the idea.

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: [SOLVED] Grouping Output Link to this post

    Awesome. UncleCheese, You're the Best!

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