Add two new accessors to ChangeSetItem

You shouldn't have to worry about exactly formatting the reference to
get a ChangeSetItem related to a DataObject, and doing it that way makes
you vulnerable to breakages like introduced in previous API change
This commit is contained in:
Hamish Friedlander 2016-04-12 08:54:38 +12:00
parent 2d16d69ddb
commit 35e642d0bf
2 changed files with 48 additions and 0 deletions

View File

@ -318,4 +318,31 @@ class ChangeSetItem extends DataObject implements Thumbnail {
// Default permissions
return (bool)Permission::checkMember($member, ChangeSet::config()->required_permission);
}
/**
* Get the ChangeSetItems that reference a passed DataObject
*
* @param DataObject $object
* @return DataList
*/
public static function get_for_object($object) {
return ChangeSetItem::get()->filter([
'ObjectID' => $object->ID,
'ObjectClass' => ClassInfo::baseDataClass($object)
]);
}
/**
* Get the ChangeSetItems that reference a passed DataObject
*
* @param int $objectID The ID of the object
* @param string $objectClass The class of the object (or any parent class)
* @return DataList
*/
public static function get_for_object_by_id($objectID, $objectClass) {
return ChangeSetItem::get()->filter([
'ObjectID' => $objectID,
'ObjectClass' => ClassInfo::baseDataClass($objectClass)
]);
}
}

View File

@ -73,4 +73,25 @@ class ChangeSetItemTest extends SapphireTest {
'Objects that have been deleted and then unpublished should return no change'
);
}
function testGetForObject() {
$object = new ChangeSetItemTest_Versioned(['Foo' => 1]);
$object->write();
$item = new ChangeSetItem([
'ObjectID' => $object->ID,
'ObjectClass' => ClassInfo::baseDataClass($object)
]);
$item->write();
$this->assertEquals(
ChangeSetItemTest_Versioned::get()->byID($object->ID)->toMap(),
ChangeSetItem::get_for_object($object)->first()->Object()->toMap()
);
$this->assertEquals(
ChangeSetItemTest_Versioned::get()->byID($object->ID)->toMap(),
ChangeSetItem::get_for_object_by_id($object->ID, $object->ClassName)->first()->Object()->toMap()
);
}
}