/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); $model->time = date("Y-m-d H:i:s"); $model->author_id = Yii::$app->user->id; $model->isdel = 0; $module = Yii::$app->getModule("yes"); $model->data = '{"1":{"type":"5","label":"weight","value":' . $module->defaults["weight"] . '}, "2":{"type":"5","label":"vat","value":' . $module->defaults["vat"] . '}}'; 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 = 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 { $model->id = array('category' => array_merge($category, [])); $model->images = $images; $model->data = json_encode($data); $transaction->rollBack(); } } catch (Exception $e) { $transaction->rollBack(); } } return $this->render('create', ['model' => $model]); }