if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->images = UploadedFile::getInstances($model, 'images');
if ($filename= $model->uploadImages()) {
$model->product_id = $id;
$model->images = $filename;
$model->save();
}
Yii::$app->session->setFlash('success', "Товар {$model->name} обновлён");
}
public function uploadImages(){
if($this->images) {
foreach ($this->images as $file) {
$filename=Yii::$app->getSecurity()->generateRandomString(15);
if ($file->saveAs('uploads/images/products/source/' . $filename . '.' . $file->extension)){
return $filename;
}
}
return true;
}else {
return false;
}
}
<?= $form->field($model, 'images[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>
public function uploadImages(){
if($this->images) {
$filesName = [];
foreach ($this->images as $file) {
$filename=Yii::$app->getSecurity()->generateRandomString(15);
if ($file->saveAs('uploads/images/products/source/' . $filename . '.' . $file->extension)){
$filesName[] = $filename;
}
}
return $filesName;
}else {
return false;
}
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->images = UploadedFile::getInstances($model, 'images');
if ($filenames= $model->uploadImages()) {
$model->product_id = $id;
if($filenames && $model->save()) {
foreach($filenames as $filename) {
$modelImage = new Image();
$modelImage->name = $filename;
$modelImage->item_id = $model->id;
... записываем еще свойства объекта картинки
if($modelImage->save()) {
Yii::$app->session->setFlash('success', "Товар {$model->name} обновлён");
}
}
}
}
}