/** * Finds the ItemImg model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return ItemImg the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ItemImg::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Finds the ItemImg model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return ItemImg the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { $itemImg = Yii::createObject(ItemImg::className()); if (($model = $itemImg::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * 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]); }
public function beforeDelete() { if (parent::beforeDelete()) { //delete images and sku ItemImg::deleteAll(['item_id' => $this->item_id]); Sku::deleteAll(['item_id' => $this->item_id]); return true; } else { return false; } }