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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Filters and sorting when getting data through a relation


Reply


3 Posts   1039 Views

Avatar
aragonne

Community Member, 26 Posts

3 April 2010 at 3:33pm

Hi there,

If I have a class that has a relation to another class and want to retrieve a DatabaObjectSet through the relation, can I also use filtering and sorting in the method call. For example:

<code>
Class myClass extends Page {

static $has_many = array { 'Students' => 'Student'; }
...
...
$mySeniorStudents = $this->Students('LasttName ASC', 'Age > 16');
}
</code>

I couldn't find any docs on this but I guess I'm trying to find something similar to the DataObject::get($obj, $filter, $sort, $join, $limit) method. If there is documentation, can you pls. provide the url?

thanks!
Steve

Avatar
Pigeon

Community Member, 243 Posts

4 April 2010 at 12:02pm

Edited: 04/04/2010 12:16pm

Yer, this is one of those 'hidden Gems'. I don't think it is documented.

But it is important to note that this returns a [url=http://doc.silverstripe.org/doku.php?id=componentset]ComponentSet[/url] not a [url=http://doc.silverstripe.org/doku.php?id=dataobjectset]DataObjectSet[/url]

PS: it is hinted at here: http://doc.silverstripe.org/doku.php?id=datamodel#custom_relation_getters

Avatar
aragonne

Community Member, 26 Posts

4 April 2010 at 1:40pm

thanks Pigeon!

Steve