Exemplo n.º 1
0
 /**
  * Lists all Images models.
  * @return mixed
  */
 public function actionIndex($itemId)
 {
     if (!Product::find()->where(['item_id' => $itemId])->exists()) {
         throw new NotFoundHttpException();
     }
     $form = new MultipleUploadForm();
     $searchModel = new ImagesSearch();
     $searchModel->item_id = $itemId;
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
     if (Yii::$app->request->isPost) {
         $form->files = UploadedFile::getInstances($form, 'files');
         if ($form->files && $form->validate()) {
             foreach ($form->files as $file) {
                 $images = new Images();
                 $images->item_id = $itemId;
                 if ($images->save()) {
                     // writes file name to images table
                     $images->big_image = $images->getUrl();
                 }
                 if ($images->save()) {
                     // saves file to folder
                     $file->saveAs($images->getPath());
                 }
             }
         }
     }
     return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'uploadForm' => $form]);
 }