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

回答編集履歴

4

修正漏れ

2017/01/29 06:58

投稿

turbgraphics200
turbgraphics200

スコア4269

answer CHANGED
@@ -8,5 +8,5 @@
8
8
  $newData = $table->newEntity();
9
9
  $newData->hoge = $hogeVal;
10
10
  $newData->fuga = $fugaVal;
11
- $this->tablModel->newData($hoge, $fuga);
11
+ $table->save($newData);
12
12
  ```

3

Controllerのみに修正

2017/01/29 06:58

投稿

turbgraphics200
turbgraphics200

スコア4269

answer CHANGED
@@ -1,21 +1,12 @@
1
1
  queryで取得した値をモデルを使用してデータに突っ込めばいいのでは?
2
2
  ```php
3
- // Model
4
- use Cake\ORM\Entity;
5
- use Cake\ORM\TableRegistry;
3
+ use Cake\ORM\TableRegistry; // useをコントローラに追加
6
- class tableModel extends Entity {
7
- protected function newData($hoge, $fuga) {
8
- $table = TableRegistry::get('table');
9
- $newData = $table->newEntity();
10
- $newData->hoge = $hogeVal;
11
- $newData->fuga = $fugaVal;
12
- }
13
- }
14
- ```
15
4
 
16
- ```php
17
- // Controller
18
5
  $hogeVal = $this->request->query('hoge');
19
6
  $fugaVal = $this->request->query('fuga');
7
+ $table = TableRegistry::get('table');
8
+ $newData = $table->newEntity();
9
+ $newData->hoge = $hogeVal;
10
+ $newData->fuga = $fugaVal;
20
11
  $this->tablModel->newData($hoge, $fuga);
21
12
  ```

2

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

2017/01/29 06:55

投稿

turbgraphics200
turbgraphics200

スコア4269

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

1

tableが抜けてた

2017/01/29 06:09

投稿

turbgraphics200
turbgraphics200

スコア4269

answer CHANGED
@@ -4,7 +4,7 @@
4
4
  $fugaVal = $this->request->query('fuga');
5
5
 
6
6
  $table = TableRegistry::get('table');
7
- $newData = $->newEntity();
7
+ $newData = $table->newEntity();
8
8
  $newData->hoge = $hogeVal;
9
9
  $newData->fuga = $fugaVal;
10
10