Multiple Insert on Form Submit

Hello everyone!,

I have a table that has 3 fields: "id" (AI,PK), "programa_id" (FK) and "spraying_id" (FK)

As you can see, on the right side it loads all the “sprayings” available and then I drag the ones I need to the left (you can see the id’s on the text field below), while the “programa” is selected on a dropdown list. So far so good.

What I need to do is to make n inserts based on the amount of “sprayings” dragged to the right, while using the same “programa” as selected on the dropdown, and obviously using auto incremental id’s.

I’m facing problems when I do the submit, the values doesn’t insert, nothing happens on DB.

This is what I got now (it’s kinda messed I think)




    public function actionCreate()

    {

        $model = new Sprayingxprograma();


        // Almacenamos el formulario desde el POST en una variable

        $post = Yii::$app->request->post();

        //var_dump($post);

        

        if (isset($post) && isset($post[$model->formName()]))

        {

            $failed = false;

            $failedCount = 0;

            // Separamos el campo dosis_receta (id separadas por comas)

            $spraying = $post[$model->formName()]['spraying_id'];

            // Transformamos el string en un array

            $spraying = explode(',', $spraying);

            

            // Recorremos el array para guardar las dosis

            $i = 0;

            foreach ($spraying as $item) {


                if (!isset($item)) { continue; }

                $i = $i+1;

                // Creamos un array con la estructura del modelo

                $sprayingxprograma = [

                        

                        'id_sprayingxprograma' => $i,

                        'spraying_id' => $item,

                        'programa_id' => $post[$model->formName()]['programa_id']

                    ];

                

                // Guardamos el modelo

                if (!($model->load($sprayingxprograma) && $model->save())) {

                    $failed = true;

                    $failedCount++;

                } 

                

            }

            

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

        } else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }



In case anyone need it, this is how I solved it:




    public function actionCreate()

    {

        $model = new Sprayingxprograma();


        $post = Yii::$app->request->post();


        if (isset($post) && isset($post[$model->formName()]))

        {

            $programa = $post[$model->formName()]['programa_id'];


            $spraying = $post[$model->formName()]['spraying_id'];


            $sprayings = explode(',', $spraying);


            $insertados = 0;


            foreach ($sprayings as $key) {

                if (!isset($key)) { continue; }


                $insertados += Yii::$app->db->createCommand()->insert('sprayingxprograma', [

                    'programa_id' => $programa,

                    'spraying_id' => $key,

                    ])->execute();

            }


            if ($insertados == count($sprayings)) {

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

            } else {

                // TODO > Informar error al usuario

                return $this->render('create', [

                    'model' => $model,

                ]);

            }

        } else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }