It seems every new project I start with Phalcon has me coming back to this same problem - related records just refuse to be saved sometimes.
I have a Program and a Course model, set up with the proper relationship:
Program:
...
public function initialize(){
$this->hasMany('id','LOID\Model\Course','program_id',['alias'=>'courses']);
}
...
Course:
...
public function initialize(){
$this->belongsTo('program_id','LOID\Model\Program','id');
}
...
In my controller, I assign a number of Courses to the Program:
if($this->request->getPost('courses')){
$courses = [];
foreach($this->request->getPost('courses') as $name){
$Course = new Course();
$Course->name = $name;
$Course->program_id = $Program->id;
$courses[] = $Course;
}
$Program->courses = $courses;
"courses" never gets saved though. As far as I can tell, this process is pretty much the same as the example here: https://docs.phalcon.io/en/latest/reference/models.html#storing-related-records. The only potential difference I can think of is that my courses
table has a multi-column primary key (name
and program_id
).
What am I doing wrong?