This class represents a set of database objects, 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.
$mySet->Count();
$myFirstDataObject = $mySet->First(); $myLastDataObject = $mySet->Last();
$mySpecialDataObjects = $mySet->find('Status', 'special'); $startingFromTen = $mySet->getOffset(10); $tenToTwenty = $mySet->getRange(10, 10);
$myIDArray = $mySet->column('ID');
You can group a set by a specific column. Consider using SQLQuery with a GROUP BY statement for enhanced performance.
$groupedSet = $mySet->groupBy('Lastname');
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
$myFirstSet->merge($mySecondSet); // $myFirstSet now contains all combined values
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);
$myArray = $mySet->toArray();
Use buildNestedUL to return a nested unordered list out of a “chain” of DataObject-relations.
It is good practice to check for empty sets before doing any iteration.
$mySet = DataObject::get('Players'); if($mySet->exists()) foreach($mySet as $player) // ...
DataObjects have native support for dealing with pagination. See setPageLimits, setPageLength, etc.
Complete pagination documentation
Please use comments for notes, tips and corrections about the described
functionality.
Use the Silverstripe Forum to
ask questions.