/** * 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) { $model = $this->findModel($id); $model->tags = !empty($model->tags) ? explode(",", $model->tags) : []; if (Yii::$app->request->post()) { $transaction = Yii::$app->db->beginTransaction(); try { $post = Yii::$app->request->post(); $category = []; $data = []; $images = []; if (isset($post['Product']['category'])) { $category = $post['Product']['category']; } if (isset($post['Product']['data'])) { $data = $post['Product']['data']; $post['Product']['data'] = json_encode($data); } if (isset($post['Product']['images'])) { $images = $post['Product']['images']; foreach ($images as $i => $img) { if (empty($img)) { unset($images[$i]); } } $post['Product']['images'] = json_encode(empty($images) ? null : $images); } if (is_array($post['Product']['tags'])) { $post['Product']['tags'] = implode(",", $post['Product']['tags']); } $model->load($post); $model->images = $model->images == "null" ? null : $model->images; if ($model->save()) { $cs = CatPro::deleteAll("product_id = :id", ["id" => $model->id]); foreach ($category as $d) { //$c = CatPro::find()->where("product_id = :id AND category_id = :aid",["id"=>$model->id,"aid"=>intval($d)])->one(); //if (!$c) //{ $c = new CatPro(); //} $c->product_id = $model->id; $c->category_id = $d; $c->isdel = 0; $c->save(); } $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } else { $transaction->rollBack(); } } catch (Exception $e) { $transaction->rollBack(); } } $model->images = json_decode($model->images); return $this->render('update', ['model' => $model]); }