回答編集履歴

4

修正漏れ

2017/01/29 06:58

投稿

turbgraphics200
turbgraphics200

スコア4267

test CHANGED
@@ -18,6 +18,6 @@
18
18
 
19
19
  $newData->fuga = $fugaVal;
20
20
 
21
- $this->tablModel->newData($hoge, $fuga);
21
+ $table->save($newData);
22
22
 
23
23
  ```

3

Controllerのみに修正

2017/01/29 06:58

投稿

turbgraphics200
turbgraphics200

スコア4267

test CHANGED
@@ -2,40 +2,22 @@
2
2
 
3
3
  ```php
4
4
 
5
- // Model
6
-
7
- use Cake\ORM\Entity;
8
-
9
- use Cake\ORM\TableRegistry;
5
+ use Cake\ORM\TableRegistry; // useをコントローラに追加
10
-
11
- class tableModel extends Entity {
12
-
13
- protected function newData($hoge, $fuga) {
14
-
15
- $table = TableRegistry::get('table');
16
-
17
- $newData = $table->newEntity();
18
-
19
- $newData->hoge = $hogeVal;
20
-
21
- $newData->fuga = $fugaVal;
22
-
23
- }
24
-
25
- }
26
-
27
- ```
28
6
 
29
7
 
30
-
31
- ```php
32
-
33
- // Controller
34
8
 
35
9
  $hogeVal = $this->request->query('hoge');
36
10
 
37
11
  $fugaVal = $this->request->query('fuga');
38
12
 
13
+ $table = TableRegistry::get('table');
14
+
15
+ $newData = $table->newEntity();
16
+
17
+ $newData->hoge = $hogeVal;
18
+
19
+ $newData->fuga = $fugaVal;
20
+
39
21
  $this->tablModel->newData($hoge, $fuga);
40
22
 
41
23
  ```

2

モデルとコントローラーに分ける

2017/01/29 06:55

投稿

turbgraphics200
turbgraphics200

スコア4267

test CHANGED
@@ -1,29 +1,41 @@
1
1
  queryで取得した値をモデルを使用してデータに突っ込めばいいのでは?
2
2
 
3
3
  ```php
4
+
5
+ // Model
6
+
7
+ use Cake\ORM\Entity;
8
+
9
+ use Cake\ORM\TableRegistry;
10
+
11
+ class tableModel extends Entity {
12
+
13
+ protected function newData($hoge, $fuga) {
14
+
15
+ $table = TableRegistry::get('table');
16
+
17
+ $newData = $table->newEntity();
18
+
19
+ $newData->hoge = $hogeVal;
20
+
21
+ $newData->fuga = $fugaVal;
22
+
23
+ }
24
+
25
+ }
26
+
27
+ ```
28
+
29
+
30
+
31
+ ```php
32
+
33
+ // Controller
4
34
 
5
35
  $hogeVal = $this->request->query('hoge');
6
36
 
7
37
  $fugaVal = $this->request->query('fuga');
8
38
 
9
-
10
-
11
- $table = TableRegistry::get('table');
12
-
13
- $newData = $table->newEntity();
39
+ $this->tablModel->newData($hoge, $fuga);
14
-
15
- $newData->hoge = $hogeVal;
16
-
17
- $newData->fuga = $fugaVal;
18
-
19
-
20
-
21
- if ($table->save($newData)) {
22
-
23
- // 追加したデータのid
24
-
25
- $id = $table->id;
26
-
27
- }
28
40
 
29
41
  ```

1

tableが抜けてた

2017/01/29 06:09

投稿

turbgraphics200
turbgraphics200

スコア4267

test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  $table = TableRegistry::get('table');
12
12
 
13
- $newData = $->newEntity();
13
+ $newData = $table->newEntity();
14
14
 
15
15
  $newData->hoge = $hogeVal;
16
16