/**
  * Creates a new ProductCategoryToProduct 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 ProductCategoryToProduct();
     if (Yii::$app->request->isPost && ($model = ProductCategoryToProduct::create(Yii::$app->request->post()))) {
         return $this->redirect(['update', 'id' => $model->id]);
     } else {
         return $this->render('create', ['username' => $username, 'model' => $model]);
     }
 }
Ejemplo n.º 2
0
 /**
  * Updates an existing Product model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $username = Yii::$app->user->identity->username;
     if ($model = $this->findModel($id)) {
         $this->product_id = $model->id;
         $model->productCategoryIds = ArrayHelper::getColumn(ProductCategoryToProduct::findAll(['product_id' => $model->id]), 'product_category_id', false);
         $model->productCollectionIds = ArrayHelper::getColumn(ProductCollectionToProduct::findAll(['product_id' => $model->id]), 'product_collection_id', false);
         $oldProductCategoryIds = $model->productCategoryIds;
         $oldProductCollectionIds = $model->productCollectionIds;
         if (Yii::$app->request->isPost && $model->update2(Yii::$app->request->post())) {
             is_array($model->productCategoryIds) or $model->productCategoryIds = [];
             foreach ($model->productCategoryIds as $product_category_id) {
                 if (!in_array($product_category_id, $oldProductCategoryIds)) {
                     ProductCategoryToProduct::create(['ProductCategoryToProduct' => ['product_category_id' => $product_category_id, 'product_id' => $model->id]]);
                 }
             }
             foreach ($oldProductCategoryIds as $product_category_id) {
                 if (!in_array($product_category_id, $model->productCategoryIds)) {
                     ProductCategoryToProduct::findOne(['product_category_id' => $product_category_id, 'product_id' => $model->id])->delete();
                 }
             }
             is_array($model->productCollectionIds) or $model->productCollectionIds = [];
             foreach ($model->productCollectionIds as $product_collection_id) {
                 if (!in_array($product_collection_id, $oldProductCollectionIds)) {
                     ProductCollectionToProduct::create(['ProductCollectionToProduct' => ['product_collection_id' => $product_collection_id, 'product_id' => $model->id]]);
                 }
             }
             foreach ($oldProductCollectionIds as $product_collection_id) {
                 if (!in_array($product_collection_id, $model->productCollectionIds)) {
                     ProductCollectionToProduct::findOne(['product_collection_id' => $product_collection_id, 'product_id' => $model->id])->delete();
                 }
             }
             return $this->goBack(Url::previous());
         } else {
             return $this->render('update', ['username' => $username, 'model' => $model]);
         }
     } else {
         throw new NotFoundHttpException();
     }
 }