Introduction

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.

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();

Output as HTML-list

Use buildNestedUL to return a nested unordered list out of a “chain” of DataObject-relations.

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

API Documentation

 
dataobjectset.txt · Last modified: 2010/05/05 23:38 (external edit)

Please use comments for notes, tips and corrections about the described functionality.
Use the Silverstripe Forum to ask questions.

blog comments powered by Disqus
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Creative Commons LicenseSilverStripe Documentation by SilverStripe is licensed under a Creative Commons Attribution 3.0 New Zealand License.
Based on a work at doc.silverstripe.com
Recent changes RSS feed Driven by DokuWiki