Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

check first write action and onAfterWrite method

Go to End

3 Posts   2621 Views


Community Member, 70 Posts

27 February 2010 at 2:55am


I would like to know if it's possible to check if a writing action is the first in onAfterWrite method ? I know it's possible in onBeforeWrite method with a test on the existance of the ID :

function onBeforeWrite() {
    if (!$this->ID) {
        // first write
    else {
        // next write when updates

But, in onAfterWrite ?



Community Member, 42 Posts

3 March 2010 at 3:28pm


so you trying to find if the DataObject you just wrote is the first in the DB?

function onAfterWrite(){
  $list = DataObject::get($this->ClassName);
  return $list->Count();

this will return number of DataObject

anyway, you don't need to check if you writing a new DataObject or just updating, th framework does it for you.


Forum Moderator, 1379 Posts

4 March 2010 at 11:18pm

Hi - the first thing that comes into my mind, without delving into any code, is - if you've check it in onBeforeWrite, you can easily save the result in a property, and check the value of that property in onAfterWrite..