Example #1
0
 /**
  * Creates a new Product model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $username = Yii::$app->user->identity->username;
     $model = new Product();
     if (Yii::$app->request->isPost && ($model = Product::create(Yii::$app->request->post()))) {
         is_array($model->productCategoryIds) or $model->productCategoryIds = [];
         foreach ($model->productCategoryIds as $product_category_id) {
             ProductCategoryToProduct::create(['ProductCategoryToProduct' => ['product_category_id' => $product_category_id, 'product_id' => $model->id]]);
         }
         is_array($model->productCollectionIds) or $model->productCollectionIds = [];
         foreach ($model->productCollectionIds as $product_collection_id) {
             ProductCollectionToProduct::create(['ProductCollectionToProduct' => ['product_collection_id' => $product_collection_id, 'product_id' => $model->id]]);
         }
         return $this->redirect(['update', 'id' => $model->id]);
     } else {
         return $this->render('create', ['username' => $username, 'model' => $model]);
     }
 }