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.

Data Model Questions /

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

onBeforeWrite called multiple times


Go to End


10 Posts   7340 Views

Avatar
BenWu

Community Member, 97 Posts

15 May 2012 at 9:10pm

Hello UncleCheese, could you explain why we shall use static class variable, not object variable??

Avatar
Jare

Community Member, 39 Posts

8 October 2016 at 2:41am

I know that this is an old thread, but as I ended up here from Google without seeing other threads in the search results, I'll use this thread.

I knew I had found a solution to this problem earlier and now that I searched for it again I went through my existing code after first searching from Google, and I found it again:

public function onBeforeWrite()
	{
		if ($this->isChanged('ID',2)) //This is a new object
		{
		}
	}

This checks if there is a real data change in the ID field (= triggers when the record is created, not anymore at the second call).

I think this is a little bit cleaner way to solve this problem than creating a static variable. Well, years have passed since the UncleCheese's answer, so perhaps this method was not available back then :).

Go to Top