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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

[Solved] Member decorator and has_one


Go to End


5 Posts   1123 Views

Avatar
theade

Community Member, 10 Posts

13 January 2014 at 5:59pm

Hello, quick question hopefully.

I'm decorating Member, adding some DB fields and a has_one relationship. Problem is that the has_one relationship is only available on one side.

class SomeDataObject extends DataObject {
static $db = array(
'Title' => 'Text'
);

static $has_one = array(
'Member' => 'Member'
);

}

and

class CustomMember extends DataExtension {

static $has_one = array(
'SomeDataObject' => 'SomeDataObject'
);

}

I'm using model admin for SomeDataObject and can add Members to SomeDataObjects. But when I look at the member in the security area, the field for SomeDataObject is empty and in the DB the SomeDataObjectID field in Member is 0.

What am I doing wrong?

Thanks

Avatar
martimiz

Forum Moderator, 1391 Posts

14 January 2014 at 1:29am

Edited: 14/01/2014 10:24am

Edit: removed obviously wrong answer.

Avatar
theade

Community Member, 10 Posts

14 January 2014 at 9:34am

Really, no one-to-one relationships? Seems a bit strange, but OK, thanks, that's good to know.

Avatar
martimiz

Forum Moderator, 1391 Posts

14 January 2014 at 10:14am

Edited: 14/01/2014 11:06am

Ok... And i'm obviously stupid today :( Sorry, please ignore what I just said about the one_to_one relation!!!

http://doc.silverstripe.com/framework/en/topics/datamodel#has-one
http://doc.silverstripe.com/framework/en/topics/datamodel#belongs-to

Two has_ones will create two foreign keys, one on each Object class, but SilverStripe will see them as two separate relations, Updating one will not automatically update the other. Belongs_to will tell SilverStripe to look for a relationship ID on the other side...

Avatar
theade

Community Member, 10 Posts

14 January 2014 at 4:49pm

Cool. That's fixed it. Cheers for your help :)