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   2480 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
banal

Community Member, 901 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
banal

Community Member, 901 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
banal

Community Member, 901 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