回答編集履歴

2

修正

2016/08/09 02:19

投稿

coba-coba
coba-coba

スコア1409

test CHANGED
@@ -10,7 +10,13 @@
10
10
 
11
11
 
12
12
 
13
+ 問題は解決したようですが、以下のコードの間違いを修正します。修正しないと気持ち悪いので…。
14
+
15
+ yajinさんの解決策通りフォームヘルパーには$entitiesを渡す方向です。
16
+
17
+
18
+
13
- 追記
19
+ 追記(修正)
14
20
 
15
21
  ---
16
22
 
@@ -66,13 +72,13 @@
66
72
 
67
73
  // POSTではない場合
68
74
 
69
- $articles = $articles->newEntity();
75
+ $entities = $articles->newEntity();
70
76
 
71
77
  }
72
78
 
73
79
 
74
80
 
75
- $this->set(compact('articles'));
81
+ $this->set(compact('entities'));
76
82
 
77
83
  }
78
84
 

1

追記

2016/08/09 02:19

投稿

coba-coba
coba-coba

スコア1409

test CHANGED
@@ -7,3 +7,73 @@
7
7
  echo $this->Form->error('user_id');
8
8
 
9
9
  ```
10
+
11
+
12
+
13
+ 追記
14
+
15
+ ---
16
+
17
+ ```PHP
18
+
19
+ public function addition5()
20
+
21
+ {
22
+
23
+ $this->ViewBuilder()->layout('auth');
24
+
25
+ $articles = TableRegistry::get('Articles');
26
+
27
+
28
+
29
+ if ($this->request->is('post')) {
30
+
31
+ // ここでバリデーションが行われる
32
+
33
+ $entities = $articles->newEntities($this->request->data());
34
+
35
+
36
+
37
+ // バリデーションでエラーが出た場合にここで処理を中断する
38
+
39
+ foreach ($entities as $entity) {
40
+
41
+ if ($entity->errors()) {
42
+
43
+ $this->set(compact('articles'));
44
+
45
+ return;
46
+
47
+ }
48
+
49
+ }
50
+
51
+
52
+
53
+ // 保存処理
54
+
55
+ $articles->connection()->transactional(function () use ($articles, $entities){
56
+
57
+ foreach ($entities as $entity) {
58
+
59
+ $articles->save($entity, ['atomic' => false]);
60
+
61
+ }
62
+
63
+ });
64
+
65
+ } else {
66
+
67
+ // POSTではない場合
68
+
69
+ $articles = $articles->newEntity();
70
+
71
+ }
72
+
73
+
74
+
75
+ $this->set(compact('articles'));
76
+
77
+ }
78
+
79
+ ```