teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

修正

2020/06/29 02:35

投稿

yakan
yakan

スコア19

title CHANGED
File without changes
body CHANGED
@@ -36,20 +36,14 @@
36
36
  try {
37
37
 
38
38
  // wp_fruits1 を保存
39
- $table = 'wp_fruits1';
40
- $data = $fruits_datas['fruits1'];
41
- $format = ['%s'];
42
- $fruits1 = $wpdb->insert( $table, $data, $format );
39
+ $fruits1 = $wpdb->insert( 'wp_fruits1', $fruits_datas['fruits1'], ['%s'] );
43
40
  if( ! $fruits1 ){
44
41
  throw new Exception("fruits1 の INSERT でエラー");
45
42
  }else{
46
43
  $fruits1_id = $wpdb->insert_id;
47
44
 
48
45
  // wp_fruits2 を保存
49
- $table = 'wp_fruits2';
50
- $data = $fruits_datas['fruits2'];
51
- $format = ['%s'];
52
- $fruits2 = $wpdb->insert( $table, $data, $format );
46
+ $fruits2 = $wpdb->insert( 'wp_fruits2', $fruits_datas['fruits2'], ['%s'] );
53
47
  if( ! $fruits2 ){
54
48
  throw new Exception("fruits2 の INSERT でエラー");
55
49
  }else{
@@ -89,20 +83,14 @@
89
83
  try {
90
84
 
91
85
  // wp_vegetables1 を保存
92
- $table = 'wp_vegetables1';
93
- $data = $vegetable_datas['vegetables1'];
94
- $format = ['%s'];
95
- $vegetables1 = $wpdb->insert( $table, $data, $format );
86
+ $vegetables1 = $wpdb->insert( 'wp_vegetables1', $vegetable_datas['vegetables1'], ['%s'] );
96
87
  if( ! $vegetables1 ){
97
88
  throw new Exception("vegetables1 の INSERT でエラー");
98
89
  }else{
99
90
  $vegetables1_id = $wpdb->insert_id;
100
91
 
101
92
  // wp_vegetables2 を保存
102
- $table = 'wp_vegetables2';
103
- $data = $vegetable_datas['vegetables2'];
104
- $format = ['%s'];
105
- $vegetables2 = $wpdb->insert( $table, $data, $format );
93
+ $vegetables2 = $wpdb->insert( 'wp_vegetables2', $vegetable_datas['wp_vegetables2'], ['%s'] );
106
94
  if( ! $vegetables2 ){
107
95
  throw new Exception("vegetables2 の INSERT でエラー");
108
96
  }else{

2

コード誤字訂正

2020/06/29 02:35

投稿

yakan
yakan

スコア19

title CHANGED
File without changes
body CHANGED
@@ -8,9 +8,9 @@
8
8
  親関数`my_insert_wp_fruits`の中で、子関数`my_insert_wp_vegetables`が実行されます。
9
9
  (それぞれ独自に実行するケースがあるため独立させています。)
10
10
 
11
- さて以下1行目の`$val`は、子関数の最後に更新される`wp_vegetables2`テーブルを更新する値ですが、ここを`CREATE`で設定した`VARCHAR(3)`以上の文字にしてエラーを起こしてみました。
11
+ さて以下5行目の`$val`は、子関数の最後に更新される`wp_vegetables2`テーブルを更新する値ですが、ここを`CREATE`で設定した`VARCHAR(3)`以上の文字にしてエラーを起こしてみました。
12
12
 
13
- しかし`ROLLBACK`が効くのは子関数で`LOCK TABLES`をかけた`wp_vegetables1`と`wp_vegetables2`だけでし
13
+ しかし`ROLLBACK`が効くのは子関数で`LOCK TABLES`をかけた`wp_vegetables1`と`wp_vegetables2`だけで、`wp_fruits1`と`wp_fruits2`はINSERTが成功てしまうのです
14
14
 
15
15
  これを親関数の`LOCK TABLES`にまでさかのぼって、`wp_fruits1`と`wp_fruits2`も含めて`ROLLBACK`させられませんでしょうか?
16
16
 
@@ -55,6 +55,7 @@
55
55
  }else{
56
56
  $fruits2_id = $wpdb->insert_id;
57
57
 
58
+ // 以下の子関数でエラーが出たとき、親関数を ROLLBACK させたい
58
59
  $vegetable_ids = my_insert_wp_vegetables($vegetable_datas);
59
60
  if( isset($vegetable_ids['error']) ){
60
61
  throw new Exception("my_insert_wp_vegetables でエラー");
@@ -62,19 +63,19 @@
62
63
  }
63
64
  }
64
65
 
65
- // 問題なければ
66
+ // 問題なければ
66
67
  $result = ['fruits1_id'=>$fruits1_id,'fruits2_id'=>$fruits2_id];
67
68
  $result = array_merge( $result, $vegetable_ids );
68
69
  $wpdb->query('COMMIT');
69
70
 
70
- } catch (Exception $e) {
71
+ } catch (Exception $e) {
71
72
  $wpdb->query('ROLLBACK');
72
- $result = $e->getMessage();
73
+ $result['error'] = $e->getMessage();
73
- } finally {
74
+ } finally {
74
- $wpdb->query('UNLOCK TABLES');
75
+ $wpdb->query('UNLOCK TABLES');
75
- }
76
+ }
76
77
 
77
- return $result;
78
+ return $result;
78
79
  }
79
80
 
80
81
  // 子関数 ( wp_vegetables1 と wp_vegetables2 を INSERT )
@@ -84,8 +85,8 @@
84
85
  $sql = "LOCK TABLES wp_vegetables1 WRITE, wp_vegetables2 WRITE";
85
86
  $wpdb->query($sql);
86
87
 
87
- $result = null;
88
+ $result = null;
88
- try {
89
+ try {
89
90
 
90
91
  // wp_vegetables1 を保存
91
92
  $table = 'wp_vegetables1';
@@ -113,14 +114,14 @@
113
114
  $result = ['vegetables1_id'=>$vegetables1_id,'vegetables2_id'=>$vegetables2_id];
114
115
  $wpdb->query('COMMIT');
115
116
 
116
- } catch (Exception $e) {
117
+ } catch (Exception $e) {
117
118
  $wpdb->query('ROLLBACK');
118
- $result = $e->getMessage();
119
+ $result['error'] = $e->getMessage();
119
- } finally {
120
+ } finally {
120
- $wpdb->query('UNLOCK TABLES');
121
+ $wpdb->query('UNLOCK TABLES');
121
- }
122
+ }
122
123
 
123
- return $result;
124
+ return $result;
124
125
  }
125
126
 
126
127
  // 4つのテーブル作成

1

加筆

2020/06/29 02:33

投稿

yakan
yakan

スコア19

title CHANGED
File without changes
body CHANGED
@@ -19,14 +19,14 @@
19
19
  create_tables();
20
20
 
21
21
  // 実行
22
- $val = 'val'; // ここを VARCHAR(3) 以上の文字にしてエラーを起こしても、wp_fruits1 と wp_fruits2 は `ROLLBACK`が効かない
22
+ $val = 'val'; // ここを VARCHAR(3) 以上の文字にしてエラーを起こしても、wp_fruits1 と wp_fruits2 は ROLLBACK が効かない
23
23
  $fruits_datas = [ 'fruits1'=>['col'=>'val'], 'fruits2'=>['col'=>'val'] ];
24
24
  $vegetable_datas = [ 'vegetables1'=>['col'=>'val'], 'vegetables2'=>['col'=>$val] ];
25
25
  $result = my_insert_wp_fruits($fruits_datas,$vegetable_datas);
26
26
  var_dump( $result );
27
27
 
28
- // wp_fruits1 と wp_fruits2 を INSERT
28
+ // 親関数 ( wp_fruits1 と wp_fruits2 を INSERT )
29
- function my_insert_wp_fruits($fruits_datas,$vegetable_datas){
29
+ function my_insert_wp_fruits($fruits_datas,$vegetable_datas=[]){
30
30
  global $wpdb;
31
31
  $wpdb->query('SET autocommit=0');
32
32
  $sql = "LOCK TABLES wp_fruits1 WRITE, wp_fruits2 WRITE";
@@ -77,7 +77,7 @@
77
77
  return $result;
78
78
  }
79
79
 
80
- // wp_vegetables1 と wp_vegetables2 を INSERT
80
+ // 子関数 ( wp_vegetables1 と wp_vegetables2 を INSERT )
81
81
  function my_insert_wp_vegetables($vegetable_datas){
82
82
  global $wpdb;
83
83
  $wpdb->query('SET autocommit=0');
@@ -139,4 +139,26 @@
139
139
  }
140
140
  }
141
141
 
142
+ ```
143
+ ###試したこと
144
+ 自分なりの解決策としては、親関数の方で、「もし子関数を更新するならLOCK TABLESを追加」というコードを下記のように加える方法ですが…
145
+
146
+ こういった方法よりも、親関数にさかのぼって`ROLLBACK`させたいと思っています。
147
+
148
+ ご指導宜しくお願い申し上げます。
149
+ ```php
150
+ function my_insert_wp_fruits($fruits_datas,$vegetable_datas=[]){
151
+ global $wpdb;
152
+ $wpdb->query('SET autocommit=0');
153
+ $sql = "LOCK TABLES wp_fruits1 WRITE, wp_fruits2 WRITE";
154
+
155
+ // もし子関数を更新するならLOCK TABLESを追加
156
+ if( !empty($vegetable_datas) ){
157
+ $sql .= ",wp_vegetables1 WRITE, wp_vegetables2 WRITE"
158
+ }
159
+
160
+ $wpdb->query($sql);
161
+
162
+ // 以下同じ
163
+ }
142
164
  ```