7921 Posts in 1359 Topics by 933 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » [SOLVED] Grouping Output
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 1308 Views |
-
[SOLVED] Grouping Output

25 August 2009 at 8:34am Last edited: 27 August 2009 8:42am
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
-
Re: [SOLVED] Grouping Output

25 August 2009 at 9:09am
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.
| 1308 Views | ||
|
Page:
1
|
Go to Top |

