Define getIDList on UnsavedRelationList

This is to fix a bug that caused CheckboxSetFields to throw an error
when trying to call this function when editing a new DataObject. This
occurred when using the advancedworkflow module.

Thanks to simonwelsh for the majority of the work on this fix.
This commit is contained in:
Robert Curry 2013-03-08 12:33:27 +13:00
parent b8e5ebb9e3
commit b9dc2dc650
2 changed files with 46 additions and 4 deletions

View File

@ -199,6 +199,26 @@ class UnsavedRelationList extends ArrayList {
$this->addMany($idList); $this->addMany($idList);
} }
/**
* Returns an array with both the keys and values set to the IDs of the records in this list.
*
* Does not return the IDs for unsaved DataObjects
*/
public function getIDList() {
// Get a list of IDs of our current items - if it's not a number then object then assume it's a DO.
$ids = array_map(function($obj) {
return is_numeric($obj) ? $obj : $obj->ID;
}, $this->items);
// Strip out duplicates and anything resolving to False.
$ids = array_filter(array_unique($ids));
// Change the array from (1, 2, 3) to (1 => 1, 2 => 2, 3 => 3)
if ($ids) $ids = array_combine($ids, $ids);
return $ids;
}
/** /**
* Returns the first item in the list * Returns the first item in the list
* *
@ -294,10 +314,6 @@ class UnsavedRelationList extends ArrayList {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList."); throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
} }
public function getIDList() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function getRange($offset, $length) { public function getRange($offset, $length) {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList."); throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
} }

View File

@ -157,6 +157,32 @@ class UnsavedRelationListTest extends SapphireTest {
array('Name' => 'C', 'Number' => 3) array('Name' => 'C', 'Number' => 3)
), $object->Siblings()); ), $object->Siblings());
} }
public function testGetIDList() {
$object = new UnsavedRelationListTest_DataObject;
$children = $object->Children();
$this->assertEquals($children->getIDList(), array());
$children->add($child1 = new UnsavedRelationListTest_DataObject(array('Name' => 'A')));
$children->add($child2 = new UnsavedRelationListTest_DataObject(array('Name' => 'B')));
$children->add($child3 = new UnsavedRelationListTest_DataObject(array('Name' => 'C')));
$children->add($child1);
$this->assertEquals($children->getIDList(), array());
$child1->write();
$this->assertEquals($children->getIDList(), array(
$child1->ID => $child1->ID
));
$child2->write();
$child3->write();
$this->assertEquals($children->getIDList(), array(
$child1->ID => $child1->ID,
$child2->ID => $child2->ID,
$child3->ID => $child3->ID
));
}
} }
class UnsavedRelationListTest_DataObject extends DataObject implements TestOnly { class UnsavedRelationListTest_DataObject extends DataObject implements TestOnly {