model() 공개 정적인 메소드

Please note that you should have this exact method in all your CActiveRecord descendants!
public static model ( string $className = __CLASS__ ) : Sku
$className string active record class name.
리턴 Sku the static model class
예제 #1
0
 public function actionUpdate()
 {
     $sku = Sku::model()->findByPk(substr($_POST['sku_id'], 3));
     if ($sku->stock < $_POST['qty']) {
         echo '<div id="error-message" style="color:red">库存数量不足。</div>';
     } else {
         $item = CartItem::model()->with('skus')->findByPk(intval($_POST['item_id']));
         $item->cartProps = empty($_POST['props']) ? '' : $_POST['props'];
         $quantity = empty($_POST['qty']) ? 1 : intval($_POST['qty']);
         Yii::app()->cart->update($item, $quantity);
     }
 }
예제 #2
0
파일: view.php 프로젝트: jackycgq/advanced
?>
</del></li>
                    <li class="price2">本店价:<em><?php 
echo $model->currency . $model->shop_price;
?>
</em></li>
                    <li class="click_count">浏览次数:<?php 
echo $model->click_count;
?>
次</li>
                    <?php 
/**
 * 显示
 */
$cri = new CDbCriteria(array('condition' => 'item_id =' . $model->item_id));
$sku_list = Sku::model()->findAll($cri);
foreach ($sku_list as $skus) {
    $props = CJSON::decode($skus->props, TRUE);
    $count = count($props);
    for ($i = 0; $i < $count; $i++) {
        $p[$i][] = $props[$i];
    }
}
for ($i = 0; $i < $count; $i++) {
    foreach (array_unique($p[$i]) as $k => $v) {
        $new_v = explode(':', $v);
        $new_arr[$new_v[0]][] = $new_v[1];
    }
}
foreach ($new_arr as $k => $v) {
    $list = ItemProp::model()->findByPk($k);
예제 #3
0
 public function actionCheckStock()
 {
     $number = $_GET['number'];
     $sku_id = $_GET['sku_id'];
     $sku = Sku::model()->findByPk($sku_id);
     $criteria = new CDbCriteria();
     $criteria->addCondition('item_id=:item_id');
     $criteria->addCondition('price=:price');
     $criteria->addCondition('props_name=:props_name');
     $criteria->params[':item_id'] = $sku->item_id;
     $criteria->params[':price'] = $sku->price;
     $criteria->params[':props_name'] = $sku->props_name;
     $orderItem = OrderItem::model()->find($criteria);
     if ($sku->stock + $orderItem->quantity < $number || $number <= 0) {
         echo 0;
     } else {
         echo 1;
     }
 }
예제 #4
0
 public function actionAjaxGetSkus()
 {
     if (!Yii::app()->request->isAjaxRequest && empty($_POST['item_id'])) {
         return;
     }
     $skus = Sku::model()->findAllByAttributes(array("item_id" => $_POST["item_id"]));
     $data = array();
     foreach ($skus as $sku) {
         $arr = array();
         $arr['sku_id'] = $sku->sku_id;
         $arr['props'] = F::convert_props_js_id($sku->props);
         //            $arr['props'] = str_replace(":","-",$arr['props']);
         $arr['price'] = $sku->price;
         $arr['stock'] = $sku->stock;
         $arr['outer_id'] = $sku->outer_id;
         $data[] = $arr;
     }
     echo json_encode($data);
 }
예제 #5
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Order();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (!$_POST['delivery_address'] && !Yii::app()->user->isGuest) {
         echo '<script>alert("您还没有添加收货地址!")</script>';
         echo '<script type="text/javascript">history.go(-1)</script>';
     } else {
         if (isset($_POST)) {
             $transaction = $model->dbConnection->beginTransaction();
             try {
                 $cart = Yii::app()->cart;
                 if (!Yii::app()->user->isGuest) {
                     $model->attributes = $_POST;
                     $model->user_id = Yii::app()->user->id ? Yii::app()->user->id : '0';
                     $cri = new CDbCriteria(array('condition' => 'contact_id =' . $_POST['delivery_address'] . ' AND user_id = ' . Yii::app()->user->id));
                     $address = AddressResult::model()->find($cri);
                     $model->receiver_country = $address->country;
                     $model->receiver_name = $address->contact_name;
                     $model->receiver_state = $address->state;
                     $model->receiver_city = $address->city;
                     $model->receiver_district = $address->district;
                     $model->receiver_address = $address->address;
                     $model->receiver_zip = $address->zipcode;
                     $model->receiver_mobile = $address->mobile_phone;
                     $model->receiver_phone = $address->phone;
                 } else {
                     $address = $_POST['AddressResult'];
                     $model->user_id = '0';
                     $model->receiver_name = $address['contact_name'];
                     $model->receiver_state = $address['state'];
                     $model->receiver_city = $address['city'];
                     $model->receiver_district = $address['district'];
                     $model->receiver_address = $address['address'];
                     $model->receiver_zip = $address['zipcode'];
                     $model->receiver_mobile = $address['mobile_phone'];
                     $model->receiver_phone = $address['phone'];
                     $model->payment_method_id = $_POST['payment_method_id'];
                     $model->memo = $_POST['memo'];
                 }
                 $model->create_time = time();
                 $model->order_id = F::get_order_id();
                 $model->total_fee = 0;
                 if (isset($_POST['keys'])) {
                     foreach ($_POST['keys'] as $key) {
                         $item = $cart->itemAt($key);
                         $model->total_fee += $item['quantity'] * $item['price'];
                     }
                 } else {
                     $item = Item::model()->findBypk($_POST['item_id']);
                     $model->total_fee = $item->price * $_POST['quantity'];
                 }
                 if ($model->save()) {
                     if (empty($_POST['keys'])) {
                         $item = Item::model()->findBypk($_POST['item_id']);
                         $sku = Sku::model()->findByPk($_POST['sku_id']);
                         if ($sku->stock < $_POST['quantity']) {
                             throw new Exception('stock is not enough!');
                         }
                         $OrderItem = new OrderItem();
                         //                            $OrderItem->order_id = $model->order_id;
                         //                            $OrderItem->item_id = $item->item_id;
                         //                            $OrderItem->title = $item->title;
                         //                            $OrderItem->desc = $item->desc;
                         //                            $OrderItem->pic = $item->getMainPic();
                         //                            $OrderItem->props_name = $sku->props_name;
                         //                            $OrderItem->price = $item->price;
                         //                            $OrderItem->quantity = $_POST['quantity'];
                         //                            $OrderItem->total_price = $OrderItem->quantity * $OrderItem->price;
                         if (!$OrderItem::model()->saveOrderItem($OrderItem, $model->order_id, $item, $sku->props_name, $_POST['quantity'])) {
                             throw new Exception('save order item fail');
                         }
                     } else {
                         foreach ($_POST['keys'] as $key) {
                             $item = $cart->itemAt($key);
                             $sku = Sku::model()->findByPk($item['sku']['sku_id']);
                             if ($sku->stock < $item['quantity']) {
                                 throw new Exception('stock is not enough!');
                             }
                             $sku->stock -= $item['quantity'];
                             if (!$sku->save()) {
                                 throw new Exception('cut down stock fail');
                             }
                             $OrderItem = new OrderItem();
                             $OrderItem->order_id = $model->order_id;
                             $OrderItem->item_id = $item['item_id'];
                             $OrderItem->title = $item['title'];
                             $OrderItem->desc = $item['desc'];
                             $OrderItem->pic = $item->getMainPic();
                             $OrderItem->props_name = $sku['props_name'];
                             $OrderItem->price = $item['price'];
                             $OrderItem->quantity = $item['quantity'];
                             $OrderItem->total_price = $OrderItem->quantity * $OrderItem->price;
                             if (!$OrderItem->save()) {
                                 throw new Exception('save order item fail');
                             }
                             $cart->remove($key);
                         }
                     }
                 } else {
                     throw new Exception('save order fail');
                 }
                 $transaction->commit();
                 $this->redirect(array('success'));
             } catch (Exception $e) {
                 $transaction->rollBack();
                 $this->redirect(array('fail'));
             }
         }
     }
 }
예제 #6
0
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer $id the ID of the model to be loaded
  * @return Sku the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = Sku::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
예제 #7
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));
 }