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, biapar, Willr, Ingo, swaiba, simon_w

check first write action and onAfterWrite method


Reply


3 Posts   1821 Views

Avatar
Myrdhin

Community Member, 67 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, 41 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, 1132 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..