Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » has_one and has_many relationship saving in a CTF

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

Page: 1
Go to End
Author Topic: 550 Views
  • copernicus
    Avatar
    Community Member
    4 Posts

    has_one and has_many relationship saving in a CTF Link to this post

    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?

  • danzzz
    Avatar
    Community Member
    175 Posts

    Re: has_one and has_many relationship saving in a CTF Link to this post

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

    550 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.