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.

Data Model Questions

onBeforeWrite() not working properly


Reply

7 Posts   2430 Views

Avatar
wee-man

17 December 2009 at 11:39pm Community Member, 21 Posts

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
banal

18 December 2009 at 3:13am Community Member, 901 Posts

Hi Michael

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

Avatar
wee-man

18 December 2009 at 11:42pm Community Member, 21 Posts

Same thing, customer id is set to default value.

Avatar
banal

19 December 2009 at 1:14am Community Member, 901 Posts

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

Avatar
wee-man

19 December 2009 at 2:00am Community Member, 21 Posts

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

Kundennummer::createKundennummer is working like expected.

Avatar
banal

19 December 2009 at 3:01am Community Member, 901 Posts

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

19 December 2009 at 3:54am Community Member, 21 Posts

Thanks, that's working.

I'm wondering because the

echo $this->Kundennummer;

output appeared...
Chase for the X-Files :)

Greetings