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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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   3030 Views

Avatar
Myrdhin

Community Member, 70 Posts

27 February 2010 at 2:55am

Hello,

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() {
    parent::onBeforeWrite();
    
    if (!$this->ID) {
        // first write
    }
    else {
        // next write when updates
    }
}

But, in onAfterWrite ?

Thanks.

Avatar
carlos

Community Member, 42 Posts

3 March 2010 at 3:28pm

salut

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

function onAfterWrite(){
  parent::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.

Avatar
martimiz

Forum Moderator, 1391 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..