Hi, I have the following relationship between models:
In the initialize method of 'Competencia' model I have
$this->hasMany('CompetenciaId', 'Competenciafuncion','CompetenciaId');
Then in 'Competenciafuncion' model initiliaze method:
$this->belongsTo('CompetenciaId', 'Competencia', 'CompetenciaId', array("foreignKey" => true));
In my program I need to update 'Competenciafuncion' records, some relations might be deleted while others might be added so I first delete related records and add the new relations like this:
$funciones = array();
$funcionesId = $this->request->getPost("funciones");
for($i=0;$i<count($funcionesId);$i++){
$funciones[$i] = new Competenciafuncion();
$funciones[$i]->FuncionId = $funcionesId[$i];
}
$competencia->competenciafuncion->delete();
$competencia->competenciafuncion = $funciones;
$competencia->save();
The delete part is working but the new relations aren't added, what am I doing wrong?