Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » onBeforeWrite and DataObjectDecorator

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

Page: 1
Go to End
Author Topic: 2370 Views
  • rbquirke
    Avatar
    Community Member
    70 Posts

    onBeforeWrite and DataObjectDecorator Link to this post

    Hi folks

    I have just been trying to get onBeforeWrite to perform some actions based on writing to a DataObjectDecorator, which in this case extends the Member DataObject.

    I had hoped this would work right off the bat, but it doesn't seem to.

    My onBeforeWrite function seems to be called ok within the DataObjectDecorator, but the objects do not seem to available to it, so no $this->ID, $this->changed etc....

    Anyone any ideas?

    Cheers

    Ronan

  • MateuszU
    Avatar
    Community Member
    89 Posts

    Re: onBeforeWrite and DataObjectDecorator Link to this post

    Use $this->owner->ID instead, that's how you access the decorated object.

    mat.

  • rbquirke
    Avatar
    Community Member
    70 Posts

    Re: onBeforeWrite and DataObjectDecorator Link to this post

    Hey mat

    Yep, but the changed + original objects do not seem to be available....

  • MateuszU
    Avatar
    Community Member
    89 Posts

    Re: onBeforeWrite and DataObjectDecorator Link to this post

    Show the code?

  • kuenkuen82
    Avatar
    Community Member
    41 Posts

    Re: onBeforeWrite and DataObjectDecorator Link to this post

    Inside the DataObjectDecorator I've created a new function which uses:

    $this->owner->ID


    fine, but how do I get to the data from the extraStatics function?

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: onBeforeWrite and DataObjectDecorator Link to this post

    Same way. Everything you add with extraStatics will become part of the decorated class:

    <?php

    class MyDataObjectDecorator extends DataObjectDecorator {
       
       function extraStatics() {
          return array(
             'db' => array(
                'MyField' => 'Varchar(255)'
             ),
             'has_one' => array(
                'SomeImage' => 'Image'
             );
       }
       
       function getLowerCasedMyField(){
          return strtolower($this->owner->MyField);   
       }
       
       function getSomeImageTitle(){
          if($this->owner->SomeImage()){
             return $this->owner->SomeImage()->Title;
          }
       }
    }

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: onBeforeWrite and DataObjectDecorator Link to this post

    rbquirke, you wont be able to access changed and original as they are private/protected respectively. However, you can use $this->owner->getChangedFields() and $this->owner->isChanged()

    2370 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.