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.

Data Model Questions

has_one and has_many relationship saving in a CTF


Reply

2 Posts   568 Views

Avatar
copernicus

30 March 2012 at 3:48am Community Member, 4 Posts

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

1 April 2012 at 11:14pm Community Member, 175 Posts

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