Hi, trying to use onAfterWrite to manually set the SortOrder on a dataobject since I'm not using the DOM to manage the objects, but rather using ModelAdmin.
Here's my code:
function onAfterWrite() {
parent::onAfterWrite();
if($this->SortOrder == 0) {
if($prevObj = DataObject::get('Photo', '', 'Created DESC', '', 1)) {
$sortOrder = ++$prevObj->SortOrder;
$this->SortOrder = $sortOrder;
$this->write();
} else {
$this->SortOrder = 1;
$this->write();
}
}
}
Basically what I'm trying to do is get the last created object, get its SortOrder, increment it and insert it in the new object.
This keeps setting the SortOrder to 1 (the else argument) even though the first argument should work. I tested the object getter using print_r and die, and it does correctly return the last created object. A little stumped here...