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

has_one and has_many relationship saving in a CTF


Go to End


2 Posts   1367 Views

Avatar
copernicus

Community Member, 4 Posts

30 March 2012 at 3:48am

I have an Event dataobject and a DateTime dataobject. The Event has a $has_many for "Showtimes" => "DateTime" and the DateTime has a $has_one for "Event" => "Event". I have these in a CTF in ModelAdmin. Right now I can create a new Event and if I add the Event then add DateTimes, everything works. The DateTimes get the EventID field populated correctly because the Event already exists in the database.

In the more likely case, a user will create a new Event, add a title, etc... then try to add some DateTimes. Since the Event does not exist in the database, there is no ID to populate EventID in the DateTimes so it gets set to 0.

Should Silverstripe handle this automatically? Is there any way to have it so the user does not need to add an Event before adding DateTimes?

Avatar
danzzz

Community Member, 175 Posts

1 April 2012 at 11:14pm

do you mean, you create an event, and then without saving add the dates and then save?
I think SS should handle that for you.

if not you can use on BeforeWrite function ... for example I use the following function to save the Parents ParentID.

function onBeforeWrite() {
		
		$r = DB::query("SELECT ParentID FROM SiteTree WHERE ID = ".$this->ParentID);		
		$this->CityPageID = $r->value();
		
		parent::onBeforeWrite();
		
	}