public function actionUpdate_ajax() { if (Yii::$app->request->isAjax) { if (isset($_POST['Product'])) { $model = $this->findModel($_POST['Product']['id']); $model->attributes = $_POST['Product']; if ($model->save()) { $ProductCategoryRel = new ProductCategoryRel(); $ProductCategoryRel->product_id = $model->id; if (empty($_POST['ProductCategoryRel'])) { ProductCategoryRel::deleteAll(['product_id' => $ProductCategoryRel->product_id]); $ProductCategoryRel->category_id = 0; $ProductCategoryRel->save(); } else { $ProductCategoryRel->category_id = $_POST['ProductCategoryRel']['category_id']; Product::update_product_category_rel($ProductCategoryRel); } $response['files'] = '<img src="' . Url::base() . '/product_uploads/' . $model->product_image[0]->image . '" alt="' . $model->product_image[0]->image . '" width="100%">'; $response['id'] = $model->id; $response['title'] = $model->title; $response['result'] = 'success'; return json_encode($response); } else { $response['result'] = 'error'; $response['files'] = Html::errorSummary($model); return json_encode($response); } } } }