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.

Archive /

Our old forums are still available as a read-only archive.

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

onAfterWrite?


Go to End


12 Posts   6415 Views

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 September 2008 at 5:50am

Is there such a thing? Best I can do is overload the write() function like so..

public function write() {
parent::write();
// do some stuff
}

The problem is, the "do some stuff" gets executed 4 times. Any idea why write() gets called so many times? Once for each relation, maybe? And what can I do about this?

Avatar
Willr

Forum Moderator, 5523 Posts

9 September 2008 at 12:20pm

There should be an onAfterWrite method you can use. Well theres a onAfterWrite() in trunk at least.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 September 2008 at 12:44pm

Cool. Any idea why the write() method runs four times on my updates? Does it run once for every related object?

What I'm trying to do is take data that is saved on the object and duplicate some of the record info into another table when the form is saved. The problem is, when the write() method gets executed 4 times, it makes 4 insertions into that table.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 September 2008 at 12:46pm

Also, while I have you here... I'm wondering, how do you specify the difference between data that was already saved on the record versus info that the user is saving into it through the form?

$this->FieldName
vs.
$this->record['FieldName']

doesn't seem to bring different results for me. Should $this->record be populated with the user's form data?

Avatar
Willr

Forum Moderator, 5523 Posts

9 September 2008 at 1:23pm

Ive just had alook at onBeforeWrite() and it looks like the current value is stored in $this->Field and the new data in $_POST['Field']. Just tried the same with onAfterWrite and it returned exactly the same - $this->Field contained the old values hmm.

Avatar
Hamish

Community Member, 712 Posts

11 September 2008 at 2:24pm

I'm experiencing similar problems - onBeforeWrite seems to execute multiple times, onAfterWrite does not appear to execute at all.

Avatar
Hamish

Community Member, 712 Posts

11 September 2008 at 2:25pm

ah, found this on another post:

//WHATCH OUT: The function onAfterWrite() is only useable when you use the Daily builds, it's not included in the Version 2.2.2
function onAfterWrite() 

Avatar
UncleCheese

Forum Moderator, 4102 Posts

12 September 2008 at 12:15am

Interesting. Thanks for that. Did we ever figure out why write() and onBeforeWrite() fire so many times?

Go to Top