silverstripe-framework/docs/en/02_Developer_Guides/00_Model/03_Lists.md

106 lines
2.3 KiB
Markdown
Raw Normal View History

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
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.
[SS_List](api:SilverStripe\ORM\SS_List) implements `IteratorAggregate`, allowing you to loop over the instance.
2014-10-27 04:40:02 +01:00
```php
2017-10-26 02:22:02 +02:00
use SilverStripe\Security\Member;
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;
}
```
2014-10-27 04:40:02 +01:00
Or in the template engine:
```ss
2017-08-07 05:11:17 +02:00
<% loop $Members %>
<!-- -->
<% end_loop %>
```
2014-10-27 04:40:02 +01:00
## Finding an item by value.
```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'
```
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
```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
```
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
```php
2017-08-07 05:11:17 +02:00
$members = Member::get();
$map = new Map($members, 'ID', 'FirstName');
```
2014-10-27 04:40:02 +01:00
## Column
```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
```
2014-10-27 04:40:02 +01:00
## ArrayList
[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
```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
```
2014-10-27 04:40:02 +01:00
## API Documentation
* [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)