예제 #1
0
 /**
  * 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.');
     }
 }
예제 #2
0
 /**
  * Creates a new Item model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = Yii::createObject(Item::className());
     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 = Yii::createObject(ItemImg::className());
                     $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]);
 }
예제 #3
0
파일: Item.php 프로젝트: ASDAFF/OurYincart2
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getItemImgs()
 {
     return $this->hasMany(ItemImg::className(), ['item_id' => 'item_id']);
 }