Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » onBeforeWrite() not working properly

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

Page: 1
Go to End
Author Topic: 2292 Views
  • wee-man
    Avatar
    Community Member
    21 Posts

    onBeforeWrite() not working properly Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: onBeforeWrite() not working properly Link to this post

    Hi Michael

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

  • wee-man
    Avatar
    Community Member
    21 Posts

    Re: onBeforeWrite() not working properly Link to this post

    Same thing, customer id is set to default value.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: onBeforeWrite() not working properly Link to this post

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

  • wee-man
    Avatar
    Community Member
    21 Posts

    Re: onBeforeWrite() not working properly Link to this post

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

    Kundennummer::createKundennummer is working like expected.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: onBeforeWrite() not working properly Link to this post

    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"]);
       }
    }

  • wee-man
    Avatar
    Community Member
    21 Posts

    Re: onBeforeWrite() not working properly Link to this post

    Thanks, that's working.

    I'm wondering because the

    echo $this->Kundennummer;

    output appeared...
    Chase for the X-Files

    Greetings

    2292 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.