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

Setting value of has_one relationship in php


Go to End


2 Posts   871 Views

Avatar
Neomang

Community Member, 9 Posts

2 June 2016 at 7:05am

I am setting up a script to automatically populate a schedule with only existing programs on a schedule each week. I've got the code down for everything except associating the image file (hosted on our servers) with the dataobject created for the series. The image is related to the dataobject via a has_one relation. I tried using the dataobject->setField() function using the name of the has_one relation as the field, but that did not work. What should I use to associate the image file with the dataobject?

Avatar
Devlin

Community Member, 344 Posts

2 June 2016 at 10:57pm

//private static $has_one = ['MyImage' => 'Image'];
$dataObject->MyImageID = $image->ID;
$dataObject->write();