Hi,
I create related records in accordance with the documentation:
$model = new Properties();
//.....
$content = [];
foreach ($this->request->getPost('params') as $param){
$params = new PropertiesParams();
$params->sort_id = $param['sort_id'];
$params->name = trim($param['name']);
$content[] = $params;
}
$model->Params = $content;
//...
if ($model->save()) {
//...
}
When user is editing these records, I'm updating changes by following way:
$model = Properties::findFirst($id);
//...
$content = [];
foreach ($this->request->getPost('params') as $param){
$params = new PropertiesParams();
$params->sort_id = $param['sort_id'];
$params->name = trim($param['name']);
$content[] = $params;
}
$model->Params->delete();
$model->Params = $content;
if ($model->save()) {
//..
}
In this case I'm deleting all old records before creating new. How to do this without deleting old records, just update them if it's needed or if user add new record - create it ?
I need to provide editing of all related records, not a single record.
Thanks for your replies.