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.

General Questions /

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

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

Filters and sorting when getting data through a relation


Go to End


3 Posts   1548 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
dhensby

Community Member, 253 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 ComponentSet not a DataObjectSet

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