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

Get Relation on data durung onBeforeWrite (first time) [SOLVED]


Go to End


5 Posts   1238 Views

Avatar
zenmonkey

Community Member, 545 Posts

2 May 2012 at 1:32pm

I have a dataobject in which I need relation data for a function on the first onBeforeWrite. The relation is set with DropdownField populated with a toDropdownMap in the CMS. In theory I should be able pull the ID from that form element but I'm not sure how its passed to the onBeforeWrite function. Calling $this->RelatedDataObejct doesn't work because that relation isn't set yet. And I can't get the form Value with $this->getField();

I could call the function I need in onAfterWrite and only fire it if the value I'm creating is null and putting another write function in it but that just seems like a recipe for disaster with potential write loops

Any ideas?

Avatar
swaiba

Forum Moderator, 1899 Posts

2 May 2012 at 7:56pm

If it is a has_one then $this->RelationNameID should work

Avatar
zenmonkey

Community Member, 545 Posts

3 May 2012 at 12:56am

The relation is still null at onBeforeWrite the first time. It doesn't get created until the actual write. I'm talking about the initial creation of the record

Avatar
swaiba

Forum Moderator, 1899 Posts

3 May 2012 at 12:59am

can you do a var_dump of $this and check for anything that matches the relationship?

we are talking a has_one relationship right?

Avatar
zenmonkey

Community Member, 545 Posts

3 May 2012 at 12:49pm

I got fixed, the DataObject in question was an extension of DataObjectAsPage so I wrapped the functions in my onBeforeWrite in a if($this->ID !=0). I'll post that it needs to be documented