Yii2.0 - 新增数据

 1 # 新增
 2 //方法一:
 3 $model_order = new PurchaseOrder();
 4 $model_order->load($res); //规则验证
 5 $model_order->operation_type  ='2';
 6 $model_order->created_at      = date('Y-m-d H:i:s');
 7 
 8 if(false==$model_order->save())
 9 {
10     $errors=$model_order->getFirstErrors(); //获取错误
11     foreach ($errors as $error)
12     {
13         $str.=$error."</br>";
14     }
15     $transactions->rollBack(); //事务:返回
16 }
17 
18 //方法二:
19 public function actionCreate()
20 {
21     $model = new GroupAuditConfig();
22     $model->cdate=time();
23     $model->uid=Yii::$app->user->id;
24 
25     if ($model->load(Yii::$app->request->post()) && $model->save()) {
26         return $this->redirect(['index']);
27     } else {
28         return $this->render('create', [
29             'model' => $model,
30         ]);
31     }
32 }
33 public function actionDelete($id)
34 {
35     $this->findModel($id)->delete();
36 }
37     -------------------------- Yii::$app->request->post() ---------------------------------
38     Array
39     (
40         [_csrf] => aFQ0XzFJWUoAYV4.WDo9BFgwTmxkHyMPCSd7LXx4FTMEIn5rRy80JA==
41         [GroupAuditConfig] => Array
42             (
43                 [group] => 1
44                 [values] => 1000
45                 [remark] => 12345678
46             )
47 
48     )
49     --------------------------------------------------------------------
50     
51 //方法三:批量保存
52 $statu = Yii::$app->db->createCommand()->batchInsert(PurchaseTemporary::tableName(),['sku','purchase_quantity', 'purchase_price','title','create_id','product_id'], $Name)->execute();
53 Yii::$app->db->createCommand()->batchInsert('pur_platform_sales_statistics',['platform','sku','warehouse_name','statistics_date','days_sales_1'],$formartval)->execute();
54     
55     
56     
57 //方法四:
58 $model = $this->findModel($id);
59 
60 if ($model->load(Yii::$app->request->post())) {
61     $model->level_audit_status=0;
62     $model->audit_note='';
63     $model->save();
64     return $this->redirect(['sales-index']);
65 }
66 protected function findModel($id)
67 {
68     if (($model = PlatformSummary::findOne($id)) !== null) {
69         return $model;
70     } else {
71         throw new NotFoundHttpException('The requested page does not exist.');
72     }
73 }

 

posted @ 2018-04-12 13:11  liuweipcs  阅读(296)  评论(0编辑  收藏  举报