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 and DataObjectDecorator


Reply

7 Posts   2541 Views

Avatar
rbquirke

25 February 2010 at 2:20pm Community Member, 70 Posts

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

Avatar
MateuszU

25 February 2010 at 2:36pm Community Member, 89 Posts

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

mat.

Avatar
rbquirke

25 February 2010 at 3:55pm Community Member, 70 Posts

Hey mat

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

Avatar
MateuszU

25 February 2010 at 4:22pm Community Member, 89 Posts

Show the code?

Avatar
kuenkuen82

17 September 2010 at 9:45pm (Last edited: 17 September 2010 9:46pm), Community Member, 41 Posts

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?

Avatar
Martijn

18 September 2010 at 12:41am Community Member, 271 Posts

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;
      }
   }
}

Avatar
simon_w

18 September 2010 at 7:32am Forum Moderator, 474 Posts

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()