예제 #1
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Sku();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Sku'])) {
         $model->attributes = $_POST['Sku'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->sku_id));
         }
     }
     $this->render('create', array('model' => $model));
 }
예제 #2
0
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     $model->scenario = 'update';
     $upload = new XUploadForm();
     $image = new ItemImg();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValisdation($model);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     $action = 'item';
     $skuIds = array();
     if (isset($_POST['Item'])) {
         $model->attributes = $_POST['Item'];
         if ($_POST['Item']['props']) {
             $model->props = CJSON::encode($_POST['Item']['props']);
         }
         if ($_POST['Item']['skus']) {
             $model->skus = CJSON::encode($_POST['Item']['skus']);
             foreach ($_POST['Item']['skus']['table'] as $s_key => $s_value) {
                 if ($s_value['sku_id'] > 0) {
                     $sku = Sku::model()->findByPk($s_value['sku_id']);
                     $sku->props = CJSON::encode($s_value['props']);
                     $sku->quantity = $s_value['quantity'];
                     $sku->price = $s_value['price'];
                     $sku->outer_id = $s_value['outer_id'];
                     $sku->status = $s_value ? 'normal' : 'deleted';
                     $sku->save();
                     $skuIds[] = $sku->sku_id;
                 } else {
                     $jsp = CJSON::encode($s_value['props']);
                     $sku = Sku::model()->findByAttributes(array("props" => $jsp, "item_id" => $model->item_id));
                     if (!$sku) {
                         $sku = new Sku();
                         $sku->item_id = $model->item_id;
                     }
                     $sku->props = $jsp;
                     $sku->quantity = $s_value['quantity'];
                     $sku->price = $s_value['price'];
                     $sku->outer_id = $s_value['outer_id'];
                     $sku->status = $s_value ? 'normal' : 'deleted';
                     $sku->save();
                     if ($sku->sku_id > 0) {
                         $skuIds[] = $sku->sku_id;
                     }
                 }
             }
             //删除
             $rawData = Sku::model()->findAll('item_id = ' . $model->item_id);
             $delArr = array();
             foreach ($rawData as $k1 => $v1) {
                 if (!in_array($v1->sku_id, $skuIds)) {
                     $delArr[] = $v1->sku_id;
                 }
             }
             if (count($delArr)) {
                 Sku::model()->updateAll(array("status" => "deleted"), 'sku_id IN (' . implode(', ', $delArr) . ')');
             }
         }
         $model->skus_data = implode(",", $skuIds);
         if ($model->save()) {
             //$this->redirect(array('view', 'id' => $model->item_id));
         }
     }
     $this->render('update', array('model' => $model, 'image' => $image, 'upload' => $upload));
 }