/**
  * @return string
  */
 public function actionIndex()
 {
     $advertisement = new Advertisement();
     $advertisement->load(Yii::$app->request->post());
     $advertisement->id_user = Yii::$app->getUser()->id;
     $advertisement->file = UploadedFile::getInstances($advertisement, 'file');
     $fileNames = [];
     if ($advertisement->file && $advertisement->validate()) {
         foreach ($advertisement->file as $file) {
             $fileName = md5(uniqid(rand(), 1));
             $fileNames[] = [$fileName];
             $file->saveAs('uploads/' . $fileName . '.' . $file->extension);
         }
         try {
             $advertisement->save();
         } catch (Exception $e) {
             return $e->getMessage();
         }
         foreach ($fileNames as $key => $value) {
             $value[] = 0;
             $value[] = $advertisement->id;
             $fileNames[$key] = $value;
         }
         try {
             Yii::$app->db->createCommand()->batchInsert(ImageAdvertisement::tableName(), ['path', 'is_logo', 'id_advertising'], $fileNames)->execute();
             $this->redirect(['update', 'id' => $advertisement->id]);
         } catch (Exception $e) {
             return $e->getMessage();
         }
     }
     return $this->render('index', ['model' => $advertisement]);
 }
示例#2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getImageAdvertisements()
 {
     return $this->hasMany(ImageAdvertisement::className(), ['id_advertising' => 'id']);
 }