The code itself

$model = new Image(); if ($model->load(Yii::$app->request->post())) { foreach($files->files as $file){ $years=date('Y'); $mounts=date('m'); $model->path = $path.'/'.$years.'/'.$mounts.'/'; $model->name = $files_to; $model->title_alt=$altNames." ".$count; $model->save(); $file->saveAs(Yii::getAlias('@frontend/web/image/') . $path . '/' . $years . '/' . $mounts . '/' . $files_to); } } 

That's how it comes out in the debager

 INSERT INTO `fl_image` (`title_alt`, `for_home`, `id_film`, `path`, `name`) VALUES ('Тарзан. Легенда 0', 0, 1, 'film/2016/07/', '12089457.jpg') UPDATE `fl_image` SET `title_alt`='Тарзан. Легенда 1', `name`='Space-Desktop.jpg' WHERE `id`=2180 UPDATE `fl_image` SET `title_alt`='Тарзан. Легенда 2', `name`='xJtqzdQr7RE.jpg' WHERE `id`=2180 

    1 answer 1

    You should enter the creation of a new $ model object in a loop like this:

     foreach ($files->files as $file) { $model = new Image(); if ($model->load(Yii::$app->request->post())) { $years = date('Y'); $mounts = date('m'); $model->path = $path.'/'.$years.'/'.$mounts.'/'; $model->name = $files_to; $model->title_alt = $altNames." ".$count; $model->save(); $file->saveAs(Yii::getAlias('@frontend/web/image/') . $path . '/' . $years . '/' . $mounts . '/' . $files_to); } }