public function store()
 {
     //
     $rules = array('name' => 'required|unique:commodities,name', 'description' => 'required', 'unit_price' => 'required|numeric', 'item_code' => 'required', 'storage_req' => 'required', 'min_level' => 'required|numeric', 'max_level' => 'required|numeric');
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return Redirect::back()->withErrors($validator);
     } else {
         // store
         $commodity = new Commodity();
         $commodity->name = Input::get('name');
         $commodity->description = Input::get('description');
         $commodity->metric_id = Input::get('unit_of_issue');
         $commodity->unit_price = Input::get('unit_price');
         $commodity->item_code = Input::get('item_code');
         $commodity->storage_req = Input::get('storage_req');
         $commodity->min_level = Input::get('min_level');
         $commodity->max_level = Input::get('max_level');
         try {
             $commodity->save();
             return Redirect::route('commodity.index')->with('message', trans('messages.commodity-succesfully-added'));
         } catch (QueryException $e) {
             Log::error($e);
         }
     }
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Commodity();
     if (isset($_POST['Commodity'])) {
         $model->attributes = $_POST['Commodity'];
         if ($_POST['Commodity']['categories'] !== '') {
             $model->categories = implode(',', $_POST['Commodity']['categories']);
         }
         //to string
         $model->save();
         $categories = $_POST['Commodity']['categories'];
         $count = count($categories);
         //$categoryNames = "";
         foreach ($categories as $category) {
             $comcat_model = new CommodityCategory();
             $comcat_model->commodity_id = $model->id;
             $comcat_model->category_id = $category;
             $temp2 = $comcat_model->commodity_id;
             $temp = $comcat_model->category_id;
             $newmodel = Category::model()->findByAttributes(array('id' => $temp));
             $newmodel3 = Category::model()->findByAttributes(array('id' => 1));
             if ($category == 1) {
                 $comcat_model->path = $newmodel->name;
             } else {
                 if ($category == 2) {
                     $comcat_model->path = $newmodel3->name . "->" . $newmodel->name;
                 } else {
                     $newmodel1 = CommodityCategory::model()->findByAttributes(array('category_id' => $temp - 1, 'commodity_id' => $temp2));
                     $comcat_model->path = $newmodel1->path . "->" . $newmodel->name;
                     //$comcat_model->path = $newmodel->name;
                 }
             }
             //$categoryNames .= $comcat_model->path.',';
             $comcat_model->save();
         }
         //$model->categoryname = $categoryNames;
         if ($model->save()) {
             $this->redirect('http://localhost/asset_management/index.php/new?commodityId=' . $model->id);
         }
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * 商品增加
  * @throws CException
  */
 public function actionAddProduct()
 {
     $commodity_model = new Commodity();
     $commodity_product_model = new Commodity_Product();
     if (isset($_POST["Commodity"]) && isset($_POST["Commodity_Product"])) {
         //foreach ($_POST["Commodity"] as $_name=>$_value) {
         //	$commodity_model->$_name = $_value;
         //}
         $commodity_model->attributes = $_POST["Commodity"];
         $commodity_product_model->attributes = $_POST["Commodity_Product"];
         //DB内的值为1和2,网页上的值为0和1
         $commodity_model->comm_kind = 2;
         //如果商品为表示的,则认定商品已经上架
         if ($commodity_model->comm_is_show == 1) {
             $commodity_model->comm_on_shelve_time = date("Y-m-d H:i:s", time());
         }
         //插入DB
         if ($commodity_model->save()) {
             //页面重定向
             //Commodity_service的表单主键设置
             $commodity_product_model->pk_prod_id = $commodity_model->pk_comm_id;
             if ($commodity_product_model->save()) {
                 $this->redirect('./index.php?r=commodity/showProduct');
             } else {
                 //删除已经插入的数据
                 $commodity_info = $commodity_model->findByPk($commodity_model->pk_comm_id);
                 $commodity_info->delete();
                 //出个警告
                 echo "<script>alert('" . $_POST["Commodity"]["comm_name"] . " 添加失败');</script>";
             }
         } else {
             echo "<script>alert('" . $_POST["Commodity"]["comm_name"] . " 添加失败');</script>";
         }
         /*echo "<pre>";
         		print_r($_POST["Commodity"]);
         		print_r($_POST["Commodity_Service"]);
         		echo "</pre>";*/
     }
     $this->renderPartial('addProduct', array('commodity_model' => $commodity_model, 'commodity_product_model' => $commodity_product_model));
 }