Saving, changes also in other tables in columns unrelated to mysql via controller functions

Hi everyone, I need to save the values ​​I am going to save on a form also on the defined columns. Should I change the form controller?

In the “main” controller I have:

public function actionCreate()
{
    $model = new Gestionepc();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id_gestionepc]);
    }

    return $this->render('create', [
        'model' => $model,
    ]);
}

public function actionUpdate($id)
{
    $model = $this->findModel($id);

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id_gestionepc]);
    }

    return $this->render('update', [
        'model' => $model,
    ]);
}

public function actionDelete($id)
{
    $this->findModel($id)->delete();

    return $this->redirect(['index']);
}

Now, at the mysql level, I should make sure that when I save and / or modify a row the value of a column of the form ‘gestionepc’ is also saved on another column of another model (in reality they would be many different values ​​in several columns to sort / save / update).

I believe that with the getDirtyAttributes () method I should solve, but also from the guide I have not had the opportunity to understand how to set it.

In practice, I have a “main” form called “gestionepc” on which I insert values ​​on all fields through each dropdownlist, these values ​​are taken from other models. In many of these “secondary” models I have inserted an unrelated column in mysql and in which I see for example that if in the “gestionepc” form I save a new row where I have entered an IP address among others, in the IP Addresses model I have a column called Associated Nr where I see the number of the associated PC. The same, inverse, happens in the Numbering model, the associated IP column will have displayed the IP address associated with that PC number, if used by the “gestionepc” form. In views it works perfectly. But on the database I find it very very difficult. I apologize for the twisted and lengthy explanation. I don’t even know if I made myself understood.

Perhaps you could use a database trigger.

2 Likes

Thanks @jimgwhit but I’m not expert using a database trigger.
I try this but not working:

CREATE DEFINER=`root`@`localhost` TRIGGER `db_gesin`.`gestionepc_ip_associato` BEFORE UPDATE ON `gestionepc` FOR EACH ROW
BEGIN
  INSERT INTO numerazioni SET
  ipass = id_indirizzip;
END