/**
  * Finds the ProductCollectionToProduct model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return ProductCollectionToProduct the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = ProductCollectionToProduct::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
示例#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();
     }
 }