Hi, I’m working with acctually unmodified gii generated code and I have a behaviour here of the beforesave function that i don’t understand.
In parallel of saving my current model named "Course" (that is populated by corresponding form) I want to save another model named "TICalendarEvent" that is partially populated with same values as course.
I decided to do this in beforesave of the course model.
First problem is that the “CalendarEvent” is saved but not the “Course” if I return false in beforesave. The form is also switching back to the input mask with the old values in the form fields. So it’s not redirected to the view of the entered dataset, what would be the gii default behaviour.
If I return true in beforesave it’s the other way round: Course is saved but NOT the CalendarEvent. (The WebApp is redirected to the view of the just entered Course dataset.)
It’s as if the $calendarEvent->save(); line is ignored. I traced down the TICaledarEvent object and found out that it is created and populated with my values… but not saved?!
Ok well first of all you must return true; from the beforeSave() in order for the model (course) in which the beforeSave method is in otherwise it will not save. That is just to be expected.
What is confusing here is that you said if it is the other way around the calendar object is not saving and I think these are two separate issues. Try using ->save(false) and see if the calendar saves then. I think the calendar might not be saving because of a validation issue. Doing ->save(false) will skip the validations rules on the object so try that.