public function actionUpdate($id) { $modelClassified = Classified::findOne($id); $modelImage = ClassifiedImage::find()->where(['classified_id' => $id])->all(); //$modelImage = $modelClassified->id; if ($modelClassified->load(Yii::$app->request->post())) { $oldIDs = \yii\helpers\ArrayHelper::map($modelImage, 'id', 'classified_id'); print_r($oldIDs); $modelImage = \common\models\Model::createMultiple(ClassifiedImage::classname(), $modelImage); \common\models\Model::loadMultiple($modelImage, Yii::$app->request->post()); $deletedIDs = array_diff($oldIDs, array_filter(\yii\helpers\ArrayHelper::map($modelImage, 'id', 'classified_id'))); // ajax validation if (Yii::$app->request->isAjax) { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return \yii\helpers\ArrayHelper::merge(\yii\widgets\ActiveForm::validateMultiple($modelImage), \yii\widgets\ActiveForm::validate($modelClassified)); } // validate all models $valid = $modelClassified->validate(); $valid = \common\models\Model::validateMultiple($modelImage) && $valid; if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $modelClassified->save(false)) { if (!empty($deletedIDs)) { ClassifiedImage::deleteAll(['id' => $deletedIDs]); } foreach ($modelImage as $i => $modelImage) { $modelImage->classified_id = $modelClassified->id; if (empty($modelImage->imageFile)) { $name = Yii::$app->security->generateRandomString(); $modelImage->imageFile = \yii\web\UploadedFile::getInstance($modelImage, "[{$i}]imageFile"); if ($modelImage->imageFile) { $modelImage->imageFile->saveAs('uploads/' . $name . '.' . $modelImage->imageFile->extension); //Upload files to server ////save path in db column $modelImage->image = 'uploads/' . $name . '.' . $modelImage->imageFile->extension; //$model->fileProfile->saveAs($model->image_profile); } } if (!($flag = $modelImage->save(false))) { $transaction->rollBack(); break; } } } if ($flag) { $transaction->commit(); return $this->refresh(); } } catch (Exception $e) { $transaction->rollBack(); } } } return $this->render('update', ['modelClassified' => $modelClassified, 'modelImage' => empty($modelImage) ? [new ClassifiedImage()] : $modelImage]); }
/** * Creates a new Parameter model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $models = [new Parameter()]; if (Yii::$app->request->post()) { $models = Model::createMultiple(Parameter::classname()); Model::loadMultiple($models, Yii::$app->request->post()); // ajax validation if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validateMultiple($models); } // validate all models $valid = Model::validateMultiple($models); if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { $flag = true; foreach ($models as $model) { if (!($flag = $model->save(false))) { $transaction->rollBack(); break; } } if ($flag) { $transaction->commit(); Yii::$app->session->setFlash('alert', ['options' => ['class' => 'alert-success'], 'body' => Yii::t('backend', 'Your data has been successfully saved')]); return $this->redirect(['index']); } } catch (Exception $e) { $transaction->rollBack(); Yii::$app->session->setFlash('alert', ['options' => ['class' => 'alert-danger'], 'body' => Yii::t('backend', 'Your data can\'t be saved')]); return $this->redirect(['index']); } } } return $this->render('create', ['models' => empty($models) ? [new Parameter()] : $models]); }
/** * Updates an existing Po 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); // // if ($model->load(Yii::$app->request->post()) && $model->save()) { // return $this->redirect(['view', 'id' => $model->id]); // } else { // return $this->render('update', [ // 'model' => $model, // ]); // } $model = $this->findModel($id); $modelsPoItems = $model->poItems; if ($model->load(Yii::$app->request->post())) { $oldIDs = ArrayHelper::map($modelsPoItems, 'id', 'id'); $modelsPoItems = Model::createMultiple(Poitem::classname(), $modelsPoItems); Model::loadMultiple($modelsPoItems, Yii::$app->request->post()); $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsPoItems, 'id', 'id'))); // ajax validation if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ArrayHelper::merge(ActiveForm::validateMultiple($modelsPoItems), ActiveForm::validate($model)); } // validate all models $valid = $model->validate(); $valid = Model::validateMultiple($modelsPoItems) && $valid; if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $model->save(false)) { if (!empty($deletedIDs)) { Poitem::deleteAll(['id' => $deletedIDs]); } foreach ($modelsPoItems as $modelPoItem) { $modelPoItem->po_id = $model->id; if (!($flag = $modelPoItem->save(false))) { $transaction->rollBack(); break; } } } if ($flag) { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } } catch (Exception $e) { $transaction->rollBack(); } } } return $this->render('update', ['model' => $model, 'modelsPoItems' => empty($modelsPoItems) ? [new Poitem()] : $modelsPoItems]); }
/** * 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) { $product = $this->findModel($id); $product->setScenario('product-update'); $ingredients = $product->ingredients; if ($product->load(Yii::$app->request->post())) { $oldIDs = ArrayHelper::map($ingredients, 'id', 'id'); $ingredients = Model::createMultiple(Ingredients::classname(), $ingredients); Model::loadMultiple($ingredients, Yii::$app->request->post()); $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($ingredients, 'id', 'id'))); // ajax validation if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ArrayHelper::merge(ActiveForm::validateMultiple($ingredients), ActiveForm::validate($product)); } $product->seen = 0; $product->sold = 0; $product->user_id = 0; // change to admin id or user id $product->created_by = Yii::$app->user->identity->username; $product->created_date = date('Y-m-d h:m:s'); $product->modified_by = Yii::$app->user->identity->username; $product->modified_date = date('Y-m-d h:m:s'); $product->status = Status::STATUS_ACTIVE; // validate all models $valid = $product->validate(); foreach ($ingredients as $detail) { $fields = array('ingredient'); $valid = $detail->validate($fields) && $valid; } if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $product->save(false)) { if (!empty($deletedIDs)) { //Ingredients::updateAll(array( 'status' => Status::STATUS_DELETED ), 'id IN ( ' . implode(",", $deletedIDs) .')' ); Ingredients::deleteAll(['id' => $deletedIDs]); } foreach ($ingredients as $ingredient) { $ingredient->created_by = Yii::$app->user->identity->username; $ingredient->created_date = date('Y-m-d h:m:s'); $ingredient->modified_by = Yii::$app->user->identity->username; $ingredient->modified_date = date('Y-m-d h:m:s'); $ingredient->status = Status::STATUS_ACTIVE; $ingredient->product_id = $product->id; if (!($flag = $ingredient->save(false))) { $transaction->rollBack(); break; } } $product->files = UploadedFile::getInstances($product, 'files'); if ($product->files) { foreach ($product->files as $file) { $productPhoto = new ProductPhoto(); $imageName = $file->baseName . substr(md5(rand()), 0, 7); $productPhoto->caption = $imageName; $productPhoto->product_id = $product->id; $productPhoto->product_photo_order = 0; $productPhoto->created_by = Yii::$app->user->identity->username; $productPhoto->created_date = date('Y-m-d h:m:s'); $productPhoto->modified_by = Yii::$app->user->identity->username; $productPhoto->modified_date = date('Y-m-d h:m:s'); $productPhoto->status = Status::STATUS_ACTIVE; $productPhoto->image_path = 'uploads/product/' . $imageName . '.' . $file->extension; if (!($flag = $productPhoto->save(false))) { $transaction->rollBack(); break; } else { $file->saveAs('uploads/product/' . $imageName . '.' . $file->extension); } } } } if ($flag) { $transaction->commit(); return $this->redirect(['view', 'id' => $product->id]); } } catch (Exception $e) { $transaction->rollBack(); } } } else { return $this->render('create', ['product' => $product, 'ingredients' => empty($ingredients) ? [new Ingredients()] : $ingredients]); } }