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

onAfterWrite


Go to End


3 Posts   883 Views

Avatar
Dorsai

Community Member, 12 Posts

7 September 2015 at 1:55pm

Hi,

I am wanting to use the onAfterWrite functionality to send some data to an API after a Page has been saved. As I understand (and can see in test scripts) when a Page is saved several writes occur (3 in my case). As I understand it the other writes are to do with related data in other tables ($has_one, $has_many, $many_many data etc).

Is there a way to identify during my onAfterWrite which is the write that saves the Page? I don't really want to trigger the sends to the API multiple times.

Cheers
D

Avatar
swaiba

Forum Moderator, 1899 Posts

9 September 2015 at 6:51pm

I'm sure I can tell you which is *the* call that writes the pages (or similar) but I can advise you to get this called once only set a flag in onBeforeWrite to true...

$this->AfterWriteFlag = true;

...then in the after write you can turn this flag off after you've done your one-time action...

if ($this->AfterWriteFlag) {
	//do stuff
	$this->AfterWriteFlag = false;
}

Avatar
Dorsai

Community Member, 12 Posts

15 September 2015 at 9:54am

Edited: 15/09/2015 9:55am

Thanks for that - I ended up doing something similar by figuring out that one write used a couple fields not used in the others and checked for those.