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

95 lines
1.8 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 [api:SS_List] instance commonly as
either [api:DataList] or [api:RelationList]. This object gives you the ability to iterate over each of the results or
modify.
## Iterating over the list.
2014-10-28 04:45:46 +01:00
[api:SS_List] implements `IteratorAggregate`, allowing you to loop over the instance.
2014-10-27 04:40:02 +01:00
:::php
$members = Member::get();
foreach($members as $member) {
echo $member->Name;
}
Or in the template engine:
:::ss
<% loop $Members %>
<!-- -->
<% end_loop %>
## Finding an item by value.
:::php
// $list->find($key, $value);
//
$members = Member::get();
echo $members->find('ID', 4)->FirstName;
// returns 'Sam'
## 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
$members = Member::get()->map('ID', 'FirstName');
// $members = array(
// 1 => 'Sam'
// 2 => 'Sig'
// 3 => 'Will'
// );
2016-09-09 08:43:05 +02:00
This functionality is provided by the [api:Map] class, which can be used to build a map around any `SS_List`.
2014-10-27 04:40:02 +01:00
:::php
$members = Member::get();
2016-09-09 08:43:05 +02:00
$map = new Map($members, 'ID', 'FirstName');
2014-10-27 04:40:02 +01:00
## Column
:::php
$members = Member::get();
echo $members->column('Email');
// returns array(
// 'sam@silverstripe.com',
// 'sig@silverstripe.com',
// 'will@silverstripe.com'
// );
## ArrayList
[api:ArrayList] exists to wrap a standard PHP array in the same API as a database backed list.
:::php
$sam = Member::get()->byId(5);
$sig = Member::get()->byId(6);
$list = new ArrayList();
$list->push($sam);
$list->push($sig);
echo $list->Count();
// returns '2'
## API Documentation
* [api:SS_List]
* [api:RelationList]
* [api:DataList]
* [api:ArrayList]
2016-09-09 08:43:05 +02:00
* [api:Map]