Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

[SOLVED] Grouping Output

Go to End

3 Posts   2001 Views


Community Member, 545 Posts

25 August 2009 at 8:34am

Edited: 27/08/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;



Forum Moderator, 4102 Posts

25 August 2009 at 9:09am

Try this:


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;

<% control Countries %>
<% control Sellers %>
<% end_control %>
<% end_control %>

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


Community Member, 545 Posts

27 August 2009 at 8:43am

Awesome. UncleCheese, You're the Best!