Saving several records using for-loop

Hi guys,

I’m totally confused. Following code will save just one record into database, although


 var_dump($anhang);

will show this:





E:\xampp\htdocs\Yii2_Mail\frontend\controllers\MailEingangController.php:331:

array (size=2)

  0 => string 'umkreis.jpg' (length=11)

  1 => string 'vollzeit.jpg' (length=12)



Obviously,


  $model_dateianhang->save();

doesn’t work as I assume to be done. Any ideas,what is wrong with this code?





    private function save_into_database($model_ausgang, $model_dateianhang, $model_e, $bez, $anhang) {

        $expression = new yii\db\Expression('NOW()');

        $now = (new \yii\db\Query)->select($expression)->scalar();

        $model_ausgang->sende_datum = $now;

        $model_ausgang->gesendet = 1;

        $model_ausgang->save();

        $model_e->id_mail_ausgang = $model_ausgang->id;

        $model_e->save();

        $model_dateianhang->id_dateianhang_art = 13;

        $model_dateianhang->bezeichnung = $bez;

        if (count($anhang) > 0) {

            for ($i = 0; $i < count($anhang); $i++) {

                $model_dateianhang->dateiname = $anhang[$i];

                $model_dateianhang->id_e_dateianhang = $model_e->id;

                $model_dateianhang->save(); //this code will be accomplished only one time-Why?

            }

        }

    }



P.S.: Only second value(vollzeit.jpg) of array will be written into database,instead both

You should create new model for each row instead of trying to reuse it.

I don’t undestand,what U mean

models are given by parameters in private function -Values are given by formular like this:





    public function actionCompose() {

        $model = new MailEingang();

        $model_ausgang = new MailAusgang();

        $model_dateianhang = new Dateianhang();

        $model_e = new EDateianhang();

        $behavior = new \common\wsl_components\Wsl_Blameable_Behavior();

        $angelegt_von = $behavior->get_User_Person_Id();

        $model_ausgang->angelegt_von = $angelegt_von;

        $model_ausgang->id_person_mitarbeiter = $angelegt_von;

        if ($model_ausgang->loadAll(Yii::$app->request->post())) {

            $model->attachement = UploadedFile::getInstances($model, 'attachement');

            $bez = "E-Mail-Anhang";

            $anhangszaehler = 0;

            $anhang = array();

            if ($model->upload()) {

                $folder_read = Yii::getAlias('@uploading/');

                $folder_write = Yii::getAlias('@emailanhang');

                $upload_copy = opendir($folder_read);

                while ($file = readdir($upload_copy)) {

                    if ($file != "." && $file != ".." && $file != ".gitkeep" && $file != ".gitignore") {

                        copy($folder_read . $file, $folder_write . $file);

                        $anhang[$anhangszaehler] = $file;

                        $anhangszaehler++;

                    }

                }

                closedir($upload_copy);

                $upload_copy = opendir($folder_read);

                while ($file = readdir($upload_copy)) {

                    if ($file != "." && $file != ".." && $file != ".gitkeep" && $file != ".gitignore") {

                        unlink($folder_read . $file);

                    }

                }

                $this->save_into_database($model_ausgang, $model_dateianhang, $model_e, $bez, $anhang);.

.

.

.



Recreating model leads to getting lost informations from formular ,won’t it?

Okay. I followed your advice and coded like this,which will do its job. But it is inexplicable for me,why loop won’t do its job. Where can I get further informations about this phenomenon





    private function save_into_database($model_ausgang, $model_dateianhang, $model_e, $bez, $anhang) {

        $expression = new yii\db\Expression('NOW()');

        $model_dateianhang_dummy = new Dateianhang();

        $model_dateianhang_dummy_dummy = new Dateianhang();

        $now = (new \yii\db\Query)->select($expression)->scalar();

        $model_ausgang->sende_datum = $now;

        $model_ausgang->gesendet = 1;

        $model_ausgang->save();

        $model_e->id_mail_ausgang = $model_ausgang->id;

        $model_e->save();

        $model_dateianhang->id_e_dateianhang = $model_e->id;

        $model_dateianhang->id_dateianhang_art = 13;

        $model_dateianhang->bezeichnung = $bez;

        $model_dateianhang_dummy->id_dateianhang_art = 13;

        $model_dateianhang_dummy->bezeichnung = $bez;

        $model_dateianhang_dummy->id_e_dateianhang = $model_e->id;

        $model_dateianhang_dummy_dummy->id_dateianhang_art = 13;

        $model_dateianhang_dummy_dummy->bezeichnung = $bez;

        $model_dateianhang_dummy_dummy->id_e_dateianhang = $model_e->id;

        if (count($anhang) == 1) {

            $model_dateianhang->dateiname = $anhang[0];

            $model_dateianhang->save();

        } else if (count($anhang) == 2) {

            $model_dateianhang->dateiname = $anhang[0];

            $model_dateianhang_dummy->dateiname = $anhang[1];

            $model_dateianhang->save();

            $model_dateianhang_dummy->save();

        } else if (count($anhang) == 3) {

            $model_dateianhang->dateiname = $anhang[0];

            $model_dateianhang_dummy->dateiname = $anhang[1];

            $model_dateianhang_dummy_dummy->dateiname = $anhang[2];

            $model_dateianhang->save();

            $model_dateianhang_dummy->save();

            $model_dateianhang_dummy_dummy->save();

        }

        return;

    }



See the source of save() - https://github.com/yiisoft/yii2/blob/master/framework/db/BaseActiveRecord.php#L643

Models take count if a record is new… after the first save() your model is not "new" anymore… so the next time it does an update() instead of insert()

For your solution you can try to call insert() instead of save()… or even better (and faster) use the query builder instead of the model… but then you don’t have validation.

Uuups. Absolutely convincingly. So , i will insert like this





        $connection = \Yii::$app->db;

        $connection->createCommand()

                ->batchInsert('dateianhang', ['', '', ''], [

                    ["","",""],

                    ["","",""],

                    ["","",""]

                ])

                ->execute();



Thx for this. This thread can be closed as succesfully solved