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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

onBeforeWrite and DataObjectDecorator


Go to End


7 Posts   3951 Views

Avatar
rbquirke

Community Member, 70 Posts

25 February 2010 at 2:20pm

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

Community Member, 89 Posts

25 February 2010 at 2:36pm

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

mat.

Avatar
rbquirke

Community Member, 70 Posts

25 February 2010 at 3:55pm

Hey mat

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

Avatar
MateuszU

Community Member, 89 Posts

25 February 2010 at 4:22pm

Show the code?

Avatar
kuenkuen82

Community Member, 41 Posts

17 September 2010 at 9:45pm

Edited: 17/09/2010 9:46pm

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

Community Member, 271 Posts

18 September 2010 at 12:41am

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

Community Member, 473 Posts

18 September 2010 at 7:32am

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