Jump to:

3431 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [Solved] Using onAfterWrite to manually set SortOrder

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1994 Views
  • Ryan M.
    Avatar
    Community Member
    309 Posts

    [Solved] Using onAfterWrite to manually set SortOrder Link to this post

    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...

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: [Solved] Using onAfterWrite to manually set SortOrder Link to this post

    Update:

    I removed the if($prevObj = DataObject::get....) part to test if it would work without checking for a previous object, but I still get the same result - the SortOrder is always set to 1.

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: [Solved] Using onAfterWrite to manually set SortOrder Link to this post

    Update #2:

    Testing further with print_r, I discovered that doing this:

    $obj = DataObject::get('Photo', false, 'Created DESC', false, 1);
          $sort = $obj->SortOrder;
          print_r($obj); die();

    Will return the object, however when I replace $obj with $sort in the print_r, it returns nothing at all. Blank! This might be why the value keeps getting set to 1, because it's incrementing on 0 or null.

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: [Solved] Using onAfterWrite to manually set SortOrder Link to this post

    Fixed, apparently it was because using DataObject::get was returning a DataObjectSet, whereas using DataObject::get_one only returned a single object instead, allowing $obj->SortOrder to be fetched.

    Final code:

    function onAfterWrite() {
          parent::onAfterWrite();
          if($this->SortOrder == 0) {
             if($prevObj = DataObject::get_one('Photo', 'SortOrder > 0')) {
                $sort = ++$prevObj->SortOrder;
                $this->SortOrder = $sort;
                $this->write();
             } else {
                $this->SortOrder = 1;
                $this->write();
             }
          }
       }

    I'll leave this thread open for posterity. =D

    1994 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.