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() not working properly


Go to End


7 Posts   3951 Views

Avatar
wee-man

Community Member, 21 Posts

17 December 2009 at 11:39pm

Hi,

i have a problem with the onBeforeWrite hook.

I want to give a customer DataObject a special customer-number (Kundennummer in german) on the first write.
Here is the code:

	function onBeforeWrite() {

		/* new Customer -> generate Customer# */
		if(!$this->ID) {
			$this->Kundennummer = Kundennummer::createKundennummer($_POST["PLZ"]);
			echo $this->Kundennummer;  //56073001 <- correct
		}

		parent::onBeforeWrite();
	}

But the result is, that the customer gets the default value (=0) as his customer-number.
Any ideas about this?

Greetings
Michael

Avatar
bummzack

Community Member, 904 Posts

18 December 2009 at 3:13am

Hi Michael

Try to move the parent::onBeforeWrite() to the beginning of your function

Avatar
wee-man

Community Member, 21 Posts

18 December 2009 at 11:42pm

Same thing, customer id is set to default value.

Avatar
bummzack

Community Member, 904 Posts

19 December 2009 at 1:14am

It should work. Can you post your class-code please? Or better: Put it on http://pastie.org/

Avatar
wee-man

Community Member, 21 Posts

19 December 2009 at 2:00am

This is my complete customer class:
http://pastie.org/private/hbmrjsytxyatbyqv84upxq

Kundennummer::createKundennummer is working like expected.

Avatar
bummzack

Community Member, 904 Posts

19 December 2009 at 3:01am

I gave this a try.
Seems like you can't use $this->ID to check if it's a new Record. This worked for me:

function onBeforeWrite() {
	
	parent::onBeforeWrite();
		
	if(!$this->Kundennummer) {
		$this->Kundennummer = Kundennummer::createKundennummer($_POST["PLZ"]);
	}
}

Avatar
wee-man

Community Member, 21 Posts

19 December 2009 at 3:54am

Thanks, that's working.

I'm wondering because the

echo $this->Kundennummer;
output appeared...
Chase for the X-Files :)

Greetings