Example #1
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getVariantI18ns()
 {
     return $this->hasMany(VariantI18n::className(), ['variant_id' => 'variant_id']);
 }
Example #2
0
 /**
  * Save variant action
  * @return array|string
  * @throws HttpException
  */
 public function actionSaveVariant()
 {
     $request = Yii::$app->request;
     if (!$request->isPost) {
         throw new HttpException(405, 'method not allowed');
     }
     if ($request->get('id') === null) {
         throw new HttpException(404, Yii::t('kalibao.backend', 'product_not_found'));
     }
     $component = new View(['models' => $this->loadEditModels(['id' => $request->get('id')]), 'language' => Yii::$app->language, 'addAgain' => $request->get('add-again', true), 'saved' => false, 'uploadConfig' => $this->uploadConfig, 'dropDownList' => function ($id) {
         return $this->getDropDownList($id);
     }]);
     foreach ($request->post('variant', []) as $id => $data) {
         $data['visible'] = isset($data['visible']) && $data['visible'] == 'on' ? 1 : 0;
         $data['top_selling'] = isset($data['top_selling']) && $data['top_selling'] == 'on' ? 1 : 0;
         $data['primary'] = isset($data['primary']) && $data['primary'] == 'on' ? 1 : 0;
         $variant = Variant::findOne($id);
         $variant->scenario = 'update-variant';
         $variant->attributes = $data;
         if (!($variantI18n = VariantI18n::findOne(['i18n_id' => Yii::$app->language, 'variant_id' => $variant->id]))) {
             $variantI18n = new VariantI18n();
         }
         $variantI18n->scenario = 'insert';
         $variantI18n->i18n_id = Yii::$app->language;
         $variantI18n->variant_id = $variant->id;
         $variantI18n->description = $data['description'];
         if (!$variant->save() || !$variantI18n->save()) {
             var_dump($variant->errors, $variantI18n->errors);
             die;
         }
     }
     TagDependency::invalidate(Yii::$app->commonCache, Product::generateTagStatic($request->get('id')));
     if ($request->isAjax) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         return ['html' => $this->renderAjax('view/_contentBlock.php', ['component' => $component, 'create' => false]), 'scripts' => $this->registerClientSideAjaxScript(), 'title' => !empty($component->models['i18n']->page_title) ? $component->models['i18n']->page_title : $component->models['i18n']->name];
     } else {
         return $this->render('view/view', ['component' => $component, 'create' => false]);
     }
 }