2014-10-27 04:40:02 +01:00
title: Managing Lists
2014-10-28 04:45:46 +01:00
summary: The SS_List interface allows you to iterate through and manipulate a list of objects.
2014-10-27 04:40:02 +01:00
# Managing Lists
2017-07-03 03:22:12 +02:00
Whenever using the ORM to fetch records or navigate relationships you will receive an [SS_List ](api:SilverStripe\ORM\SS_List ) instance commonly as
either [DataList ](api:SilverStripe\ORM\DataList ) or [RelationList ](api:SilverStripe\ORM\RelationList ). This object gives you the ability to iterate over each of the results or
2014-10-27 04:40:02 +01:00
modify.
## Iterating over the list.
2017-07-03 03:22:12 +02:00
[SS_List ](api:SilverStripe\ORM\SS_List ) implements `IteratorAggregate` , allowing you to loop over the instance.
2014-10-27 04:40:02 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-08-07 05:11:17 +02:00
$members = Member::get();
2014-10-27 04:40:02 +01:00
2017-08-07 05:11:17 +02:00
foreach($members as $member) {
echo $member->Name;
}
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
Or in the template engine:
2017-08-03 02:51:32 +02:00
```ss
2017-08-07 05:11:17 +02:00
< % loop $Members %>
<!-- -->
< % end_loop %>
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
## Finding an item by value.
2017-08-03 02:51:32 +02:00
```php
2017-08-07 05:11:17 +02:00
// $list->find($key, $value);
2014-10-27 04:40:02 +01:00
2017-08-07 05:11:17 +02:00
//
$members = Member::get();
2014-10-27 04:40:02 +01:00
2017-08-07 05:11:17 +02:00
echo $members->find('ID', 4)->FirstName;
// returns 'Sam'
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
## Maps
A map is an array where the array indexes contain data as well as the values. You can build a map from any list
2017-08-03 02:51:32 +02:00
```php
2017-08-07 05:11:17 +02:00
$members = Member::get()->map('ID', 'FirstName');
// $members = array(
// 1 => 'Sam'
// 2 => 'Sig'
// 3 => 'Will'
// );
2017-08-03 05:35:09 +02:00
2017-08-03 02:51:32 +02:00
```
2017-07-03 03:22:12 +02:00
This functionality is provided by the [Map ](api:SilverStripe\ORM\Map ) class, which can be used to build a map around any `SS_List` .
2014-10-27 04:40:02 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-08-07 05:11:17 +02:00
$members = Member::get();
$map = new Map($members, 'ID', 'FirstName');
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
## Column
2017-08-03 02:51:32 +02:00
```php
2017-08-07 05:11:17 +02:00
$members = Member::get();
2014-10-27 04:40:02 +01:00
2017-08-07 05:11:17 +02:00
echo $members->column('Email');
2014-10-27 04:40:02 +01:00
2017-08-07 05:11:17 +02:00
// returns array(
// 'sam@silverstripe.com',
// 'sig@silverstripe.com',
// 'will@silverstripe.com'
// );
2017-08-03 05:35:09 +02:00
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
## ArrayList
2017-07-03 03:22:12 +02:00
[ArrayList ](api:SilverStripe\ORM\ArrayList ) exists to wrap a standard PHP array in the same API as a database backed list.
2014-10-27 04:40:02 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-08-07 05:11:17 +02:00
$sam = Member::get()->byId(5);
$sig = Member::get()->byId(6);
2014-10-27 04:40:02 +01:00
2017-08-07 05:11:17 +02:00
$list = new ArrayList();
$list->push($sam);
$list->push($sig);
2014-10-27 04:40:02 +01:00
2017-08-07 05:11:17 +02:00
echo $list->Count();
// returns '2'
2017-08-03 05:35:09 +02:00
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
## API Documentation
2017-07-03 03:22:12 +02:00
* [SS_List ](api:SilverStripe\ORM\SS_List )
* [RelationList ](api:SilverStripe\ORM\RelationList )
* [DataList ](api:SilverStripe\ORM\DataList )
* [ArrayList ](api:SilverStripe\ORM\ArrayList )
* [Map ](api:SilverStripe\ORM\Map )