public function actionUpload_product() { if (Yii::$app->request->isAjax) { $model = new ProductCategory(); $Product = new Product(); $ProductImageRel = new ProductImageRel(); $ProductCategoryRel = new ProductCategoryRel(); $model_cat_title = UploadedFile::getInstance($model, 'cat_title'); $time = time(); $model_cat_title->saveAs('product_uploads/' . $time . $model_cat_title->baseName . '.' . $model_cat_title->extension); if ($model_cat_title) { $response = []; $Product->title = $_POST['title']; $Product->desc = $_POST['desc']; $Product->status = 1; if ($Product->save()) { $ProductCategoryRel->category_id = $_POST['id']; $ProductCategoryRel->product_id = $Product->id; $ProductImageRel->product_id = $Product->id; $ProductImageRel->image = $time . $model_cat_title->baseName . '.' . $model_cat_title->extension; if ($ProductCategoryRel->save() && $ProductImageRel->save()) { $response['files'][] = ['name' => $time . $model_cat_title->name, 'type' => $model_cat_title->type, 'size' => $model_cat_title->size, 'url' => Url::base() . '/product_uploads/' . $time . $model_cat_title->baseName . '.' . $model_cat_title->extension, 'deleteUrl' => Url::to(['delete_uploaded_file', 'file' => $model_cat_title->baseName . '.' . $model_cat_title->extension]), 'deleteType' => 'DELETE']; $response['base'] = $time . $model_cat_title->baseName; $response['view'] = $this->renderAjax('uploaded_product', ['url' => Url::base() . '/product_uploads/' . $time . $model_cat_title->baseName . '.' . $model_cat_title->extension, 'basename' => $time . $model_cat_title->baseName, 'id' => $ProductImageRel->id, 'model' => $Product]); } } else { $response['errors'] = $product->getErrors(); } return json_encode($response); } } }
public function getproduct_image() { return $this->hasMany(ProductImageRel::className(), ['product_id' => 'id'])->orderBy('sort_order asc'); }
/** * Deletes an existing Product model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { ProductCategoryRel::deleteAll(['product_id' => $id]); $images = ProductImageRel::findAll(['product_id' => $id]); if (!empty($images)) { foreach ($images as $key => $value) { unlink(\Yii::getAlias('@webroot') . '/product_uploads/' . $value->image); } } ProductImageRel::deleteAll(['product_id' => $id]); $files = ProductFiles::findAll(['product_id' => $id]); if (!empty($files)) { foreach ($files as $key => $value) { unlink(\Yii::getAlias('@webroot') . '/product_files/' . $value->file_name); } } ProductFiles::deleteAll(['product_id' => $id]); ProductSpecification::deleteAll(['product_id' => $id]); $this->findModel($id)->delete(); return $this->redirect(['index']); }