This document contains information for an outdated version (2.3) and may not be maintained any more.
If some of your projects still use this version, consider upgrading as soon as possible.
DataObjectSet
Introduction
This class represents a set of DataObjects, such as the results of a query. It is the base for all datamodel-related querying. It implements the Iterator interface introduced in PHP5.
Relations (has_many/many_many) are described in ComponentSet, a subclass of DataObjectSet.
Usage
Getting the size
$mySet->Count();
Getting an single element
$myFirstDataObject = $mySet->First(); $myLastDataObject = $mySet->Last();
Getting multiple elements
$mySpecialDataObjects = $mySet->find('Status', 'special');
$startingFromTen = $mySet->getOffset(10);
$tenToTwenty = $mySet->getRange(10, 10);
Getting one property
$myIDArray = $mySet->column('ID');
Grouping
You can group a set by a specific column. Consider using SQLQuery with a GROUP BY statement for enhanced performance.
$groupedSet = $mySet->groupBy('Lastname');
Sorting
Sort a set by a specific column.
$mySet->sort('Lastname'); //ascending
$mySet->sort('Lastname', 'DESC'); //descending
This works on the object itself, so do NOT do something like this:
$sortedSet = $mySet->sort('Lastname'); //ascending
Merge with other DataObjectSets
$myFirstSet->merge($mySecondSet); // $myFirstSet now contains all combined values
Mapping for Dropdowns
When using DropdownField and its numerous subclasses to select a value from a set, you can easily map the records to a compatible array:
$map = $mySet->toDropDownMap('ID', 'Title');
$dropdownField = new DropdownField('myField', 'my label', $map);
Converting to array
$myArray = $mySet->toArray();
Checking for existence
It is good practice to check for empty sets before doing any iteration.
$mySet = DataObject::get('Players');
if($mySet->exists()) foreach($mySet as $player)
Paging
DataObjects have native support for dealing with pagination. See setPageLimits, setPageLength, etc.
FIXME Complete pagination documentation

Comments
Comment policy: Please use comments for tips and corrections about the described functionality.
blog comments powered by DisqusComments are moderated, we reserve the right to remove comments that are inappropriate or are no longer relevant. Use the Silverstripe Forum to ask questions.