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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

sorting by related fields


Go to End


6 Posts   1969 Views

Avatar
tmkp

Community Member, 42 Posts

9 January 2010 at 9:34am

Edited: 09/01/2010 10:03am

Hallo zusammen,

my first post here on the forums. First of all, i'm really impressed with SilverStripe and all the work that you guys have put into this.. I've come quite a long way looking for a flexible (and pretty : ) CMS, and although right now i feel a little dizzy from crash-coursing myself into the system i think it'll be perfect for my next projects. So i thought a thanks would be in order here..

My question relates to the DOM (which seems to be a great tool by the way, UncleCheese), and is somewhat related to this post
http://www.silverstripe.org/dataobjectmanager-module-forum/show/267254#post267254
as well as this one
http://www.silverstripe.org/dataobjectmanager-module-forum/show/275956#post275956

I'm trying to create an "Affiliates" Page Class which stores affiliates (for now, only name & URL) divided into categories, so much like the Resource Pages from the Tutorial. Only should the categories also come from a DataObject, so the website owner can add/remove categories dynamically.

Here's my code so far

### AffiliateDataObject.php

<?php

class AffiliateDataObject extends DataObject {

	static $db = array (
		'Name' => 'Text',
		'Url' => 'Text'
	);

	static $has_one = array (
		'AffiliatePage' => 'AffiliatePage',
		'AffiliateCategory' => 'AffiliateCategory'
	);

	// Testing, obsolete?
	function getMyCategoryName() {
	return $this->AffiliateCategory()->Name;
	}

	function getCMSFields_forPopup() {
		$fields = new FieldSet();
		$fields->push( new TextField( 'Name', 'Name' ) );
		$fields->push( new TextField( 'Url', 'Url' ) );

		$oData = (DataObject::get('AffiliateCategory'));
		if ($oData) $CategoriesSource = $oData->toDropDownMap('ID','Name');
		$dropdown = new DropdownField('AffiliateCategoryID','Category',$CategoriesSource);
		$dropdown->setEmptyString('-- Select category --');
		$fields->push ( $dropdown );

		return $fields;
	}

}


### AffiliateCategory.php

<?php

class AffiliateCategory extends DataObject {

	static $db = array (
		'Name' => 'Varchar(100)'
	);

	static $has_one = array (
		'AffiliatePage' => 'AffiliatePage'
	);

	static $has_many = array (
	  'AffiliateDataObjects' => 'AffiliateDataObject'
	);

	public function getCMSFields_forPopup() {
    return new FieldSet(
  			new TextField('Name', 'Name')
  	);
  }

}


### AffiliatePage.php

<?php

class AffiliatePage extends Page {

	static $db = array (
	);

	static $has_many = array (
		'AffiliateDataObjects' => 'AffiliateDataObject',
		'AffiliateCategories' => 'AffiliateCategory'
	);

	function getCMSFields() {
		$f = parent::getCMSFields();

		$affiliate_manager = new DataObjectManager(
			$this,
			'AffiliateDataObjects',
			'AffiliateDataObject',
			array('Name' => 'Name','Url'=>'Url','AffiliateCategory.Name'=>'Category'),
			'getCMSFields_forPopup'
		);

		$affiliate_manager->setPerPageMap(array('50'));

		$f->addFieldToTab("Root.Content.Affiliates", $affiliate_manager);

		$f->addFieldToTab("Root.Content.Categories", new DataObjectManager(
			$this,
			'AffiliateCategories',
			'AffiliateCategory',
			array('Name'=>'Name'),
			'getCMSFields_forPopup'
		));

		return $f;

	}

}


class AffiliatePage_Controller extends Page_Controller {

}

Everything seems to be working just fine, i can add categories and get a dropdown menu showing the categories name when i add a new "Affiliate" Data Object.

However there's no sorting by category in the DOM. If I click the header area in DOM for "Category" the loading animation appears and disappears, but no sorting occurs.

In an ideal case, what i'm trying to achieve is alphabetcal ordering by category name (which has to be pulled from the AffiliateCategory DataObject)

I tried changing

### AffiliatePage.php

array('Name' => 'Name','Url'=>'Url','MyCategoryName'=>'Category'),

to

array('Name' => 'Name','Url'=>'Url','AffiliateCategory.Name'=>'Category'),

which also outputs the categories column correctly, but renders the table header unclickable, so also no sorting.

Could this possibly have to do with naming conventions for relationships that (while unbeknownst to me) make the whole thing work automagically?

I've also been trying to gather more documentation about working with related field values in DOM, does anybody have some tutorial or working module recommendations for this kind of thing?

Any help would be greatly appreciated.. Thanks and all the best,
Andi

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 January 2010 at 10:06am

Yeah, that's unsupported at the moment. I'm surprised you were able to click the column at all. Related fields should not be sortable. You should be able to add a sort clause to your DOM constructor, though. Did that not work? You'll lose the interactivity, but at least you'd have the alpha sort you want. That and a custom filter dropdown should get you pretty far along.

Avatar
tmkp

Community Member, 42 Posts

9 January 2010 at 10:33am

Edited: 09/01/2010 10:34am

Hi UncleCheese,
thanks for the quick reply!

I'm afraid i'm not quite sure how to apply the sort clause though. Putting

### AffiliatePage.php 

		$affiliate_manager = new DataObjectManager(
			$this,
			'AffiliateDataObjects',
			'AffiliateDataObject',
			array('Name' => 'Name','Url'=>'Url','AffiliateCategory.Name'=>'Category'),
			'getCMSFields_forPopup',
			null,
			"AffiliateCategory.Name ASC" // Sort by name
		);

throws an error when trying to load the page in the cms. Any pointers at how to implement this?

Thanks again!
Andi

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 January 2010 at 10:59am

What is the error?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 January 2010 at 10:59am

Oh, right.. just realized, you'll need to use the join clause, too. Make sure you're joining the category table on your object.

Avatar
tmkp

Community Member, 42 Posts

9 January 2010 at 11:28am

Hi,
sorry, the error was an empty javascript alert. The page didn't load at all.

Trying to get my head around the join clause, but dealing with those was basically what i've been trying to avoid all my life : )

I'm pretty lost with this right now, going to take another look at the code tomorrow. Is there any hope that sorting by related fields will be supported anytime soon (or late)?