mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
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:
parent
2d16d69ddb
commit
35e642d0bf
@ -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)
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user