/** * 商品增加 * @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)); }