/** * Creates a new ItemImg model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $itemModel = new Item(); $imagesArray = $itemModel->getUploadImages(); foreach ($imagesArray as $image) { $itemImg = new ItemImg(); $itemImg->item_id = Yii::$app->request->post('item_id'); $itemImg->pic = $image['pic']; $itemImg->title = $image['title']; $itemImg->position = Yii::$app->request->post('position'); $itemImg->create_time = time(); if (!$itemImg->save()) { return json_encode(['error' => Yii::t('catalog', 'save images to database fail.')]); } } return json_encode([]); }
/** * Creates a new Item model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Item(); if ($model->load(Yii::$app->request->post())) { $transaction = Yii::$app->db->beginTransaction(); $itemData = $this->handlePostData($model); /** @var $model \star\catalog\models\Item */ $model = $itemData[0]; $skus = $itemData[1]; if (!$skus) { return $this->render('create', ['model' => $model]); } if ($model->save()) { $model->saveSkus($model->item_id, $skus); $imagesArray = $model->getUploadImages(); foreach ($imagesArray as $num => $image) { if ($image) { $itemImg = new ItemImg(); $itemImg->item_id = $model->item_id; $itemImg->pic = $image['pic']; $itemImg->title = $image['title']; $itemImg->position = $num; $itemImg->create_time = time(); if (!$itemImg->save()) { $model->addError('images', Yii::t('catalog', 'save images to database fail.')); } } } } if (!$model->hasErrors()) { $transaction->commit(); return $this->redirect(['index']); } $transaction->rollBack(); } return $this->render('create', ['model' => $model]); }