diff --git a/forms/LookupField.php b/forms/LookupField.php index d68d9319d..d9c9a563d 100644 --- a/forms/LookupField.php +++ b/forms/LookupField.php @@ -17,13 +17,15 @@ class LookupField extends DropdownField { // Normalize value to array to simplify further processing - $values = (is_array($this->value)) ? $this->value : array(trim($this->value)); + $values = (is_array($this->value) || is_object($this->value)) ? $this->value : array(trim($this->value)); $mapped = array(); if($source instanceof SQLMap) { foreach($values as $value) $mapped[] = $source->getItem($value); - } elseif(is_array($source)) { - $mapped = array_intersect_key($source, array_combine($values, $values)); + } else if($source instanceof ArrayAccess || is_array($source)) { + foreach($values as $value) { + if(isset($source[$value])) $mapped[] = $source[$value]; + } } else { $mapped = array(); } diff --git a/model/fieldtypes/ForeignKey.php b/model/fieldtypes/ForeignKey.php index 3042af1cb..e84ebebfb 100644 --- a/model/fieldtypes/ForeignKey.php +++ b/model/fieldtypes/ForeignKey.php @@ -44,7 +44,7 @@ class ForeignKey extends Int { } } else { $titleField = (singleton($hasOneClass)->hasField('Title')) ? "Title" : "Name"; - $map = new SQLMap(singleton($hasOneClass)->extendedSQL(), "ID", $titleField); + $map = DataList::create($hasOneClass)->map("ID", $titleField); $field = new DropdownField($this->name, $title, $map, null, null, ' '); } diff --git a/model/fieldtypes/PrimaryKey.php b/model/fieldtypes/PrimaryKey.php index a3c4d318d..af767b5c6 100644 --- a/model/fieldtypes/PrimaryKey.php +++ b/model/fieldtypes/PrimaryKey.php @@ -26,7 +26,7 @@ class PrimaryKey extends Int { public function scaffoldFormField($title = null, $params = null) { $titleField = ($this->object->hasField('Title')) ? "Title" : "Name"; - $map = new SQLMap($this->object->extendedSQL(), "ID", $titleField); + $map = DataList::create(get_class($this->object))->map("ID", $titleField); return new DropdownField($this->name, $title, $map, null, null, ' '); } } diff --git a/security/Group.php b/security/Group.php index 353f596ae..994f35c57 100644 --- a/security/Group.php +++ b/security/Group.php @@ -221,16 +221,15 @@ class Group extends DataObject { return $this->getManyManyComponents('Members'); } - public function map($filter = "", $sort = "", $blank="") { - $ret = new SQLMap(singleton('Group')->extendedSQL($filter, $sort)); - if($blank){ - $blankGroup = new Group(); - $blankGroup->Title = $blank; - $blankGroup->ID = 0; + public static function map($filter = "", $sort = "", $blank="") { + Deprecation::notice('3.0', 'Use DataList::("Group")->map()'); - $ret->getItems()->unshift($blankGroup); - } - return $ret; + $list = DataList::create("Group")->where($filter)->sort($sort); + $map = $list->map(); + + if($blank) $map->unshift(0, $blank); + + return $map; } /** diff --git a/security/Member.php b/security/Member.php index af7b1f32c..5708c7df0 100644 --- a/security/Member.php +++ b/security/Member.php @@ -971,17 +971,15 @@ class Member extends DataObject { * * @todo Improve documentation of this function! (Markus) */ - public function map($filter = "", $sort = "", $blank="") { - $ret = new SQLMap(singleton('Member')->extendedSQL($filter, $sort)); - if($blank) { - $blankMember = Object::create('Member'); - $blankMember->Surname = $blank; - $blankMember->ID = 0; + public static function map($filter = "", $sort = "", $blank="") { + Deprecation::notice('3.0', 'Use DataList::("Member")->map()'); - $ret->getItems()->unshift($blankMember); - } + $list = DataList::create("Member")->where($filter)->sort($sort); + $map = $list->map(); + + if($blank) $map->unshift(0, $blank); - return $ret; + return $map; } diff --git a/tests/forms/TableFieldTest.php b/tests/forms/TableFieldTest.php index dc68c44a6..5662cd114 100644 --- a/tests/forms/TableFieldTest.php +++ b/tests/forms/TableFieldTest.php @@ -55,7 +55,7 @@ class TableFieldTest extends SapphireTest { $this->assertEquals(array( 1 => 'CustomPerm1', 2 => 'CustomPerm2', - ), $permissions); + ), $permissions->toArray()); // Test repeated insert @@ -80,7 +80,7 @@ class TableFieldTest extends SapphireTest { 1 => 'CustomPerm1', 2 => 'CustomPerm2', 3 => 'CustomPerm3', - ), $permissions); + ), $permissions->toArray()); } @@ -130,7 +130,7 @@ class TableFieldTest extends SapphireTest { $this->assertEquals(array( 101 => 'Perm1 Modified', 102 => 'Perm2 Modified', - ), $permissions); + ), $permissions->toArray()); } function testDelete() { diff --git a/tests/forms/TableListFieldTest.php b/tests/forms/TableListFieldTest.php index dbc8ad09a..9898d8fb6 100644 --- a/tests/forms/TableListFieldTest.php +++ b/tests/forms/TableListFieldTest.php @@ -59,7 +59,7 @@ class TableListFieldTest extends SapphireTest { $item3->ID => "a3", $item4->ID => "a4", $item5->ID => "a5" - ), $itemMap); + ), $itemMap->toArray()); } function testFirstPageOfPaginatedSourceItemGeneration() { @@ -92,7 +92,7 @@ class TableListFieldTest extends SapphireTest { $this->assertEquals(array( $item1->ID => "a1", $item2->ID => "a2" - ), $itemMap); + ), $itemMap->toArray()); } function testSecondPageOfPaginatedSourceItemGeneration() { @@ -123,7 +123,7 @@ class TableListFieldTest extends SapphireTest { $this->assertNotNull($items); $itemMap = $items->map("ID", "A") ; - $this->assertEquals(array($item3->ID => "a3", $item4->ID => "a4"), $itemMap); + $this->assertEquals(array($item3->ID => "a3", $item4->ID => "a4"), $itemMap->toArray()); } function testSelectOptionsAddRemove() { diff --git a/tests/model/DataListTest.php b/tests/model/DataListTest.php index a9aca7127..90f13d8e1 100644 --- a/tests/model/DataListTest.php +++ b/tests/model/DataListTest.php @@ -101,10 +101,10 @@ class DataListTest extends SapphireTest { } function testMap() { - $map = DataList::create('DataObjectTest_TeamComment')->map(); + $map = DataList::create('DataObjectTest_TeamComment')->map()->toArray(); $expected = array(1=>'Joe', 2=>'Bob', 3=>'Phil'); $this->assertEquals($expected, $map); - $otherMap = DataList::create('DataObjectTest_TeamComment')->map('Name', 'TeamID'); + $otherMap = DataList::create('DataObjectTest_TeamComment')->map('Name', 'TeamID')->toArray(); $otherExpected = array ('Joe' => '1','Bob' => '1','Phil' => '2'); $this->assertEquals($otherExpected, $otherMap); } diff --git a/tests/security/GroupTest.php b/tests/security/GroupTest.php index cf3d9df24..873fb55d6 100644 --- a/tests/security/GroupTest.php +++ b/tests/security/GroupTest.php @@ -37,7 +37,7 @@ class GroupTest extends FunctionalTest { // We will iterate over the map and build mapOuput to more easily call assertions on the result. $map = Group::map(); - $mapOutput = $map->getItems()->map('ID', 'Title'); + $mapOutput = $map->toArray(); $group1 = $this->objFromFixture('Group', 'group1'); $group2 = $this->objFromFixture('Group', 'group2');