質問編集履歴

5

誤字修正

2017/06/20 08:16

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
  Post.php(Model)で、$hasManyのforeignKeyに「**post_id**(Commentテーブルのカラム)」を設定し、
32
32
 
33
- **###Postテーブルのid = Commentテーブルのpost_id**
33
+ **Postテーブルのid = Commentテーブルのpost_id**
34
34
 
35
35
  という状況になっているかと思います。
36
36
 
@@ -60,7 +60,7 @@
60
60
 
61
61
 
62
62
 
63
- ****〇view****【hiddenでpost_idを持たせたい(Postテーブルのid)】
63
+ **〇view**【hiddenでpost_idを持たせたい(Postテーブルのid)】
64
64
 
65
65
  <?php
66
66
 
@@ -78,7 +78,7 @@
78
78
 
79
79
 
80
80
 
81
- ****〇Controller**__**【viewで設定したhiddenの値を受け取ってCommnetテーブルの「post_id」カラムに格納したい】__
81
+ **〇Controller**【viewで設定したhiddenの値を受け取ってCommnetテーブルの「post_id」カラムに格納したい】__
82
82
 
83
83
  public function add() {
84
84
 

4

誤字修正

2017/06/20 08:16

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -60,13 +60,13 @@
60
60
 
61
61
 
62
62
 
63
- **〇view**【hiddenでpost_idを持たせたい(Postテーブルのid)】
63
+ ****〇view****【hiddenでpost_idを持たせたい(Postテーブルのid)】
64
64
 
65
65
  <?php
66
66
 
67
67
  echo $this->Form->create('Comment',array('url'=>array('controller' => 'comments', 'action' => 'add')));
68
68
 
69
- echo $this->Form->hidden('Comment.post_id');
69
+ echo $this->Form->hidden($post['Post']['id']);
70
70
 
71
71
  echo $this->Form->input('name',array('label'=>'名前'));
72
72
 
@@ -78,7 +78,7 @@
78
78
 
79
79
 
80
80
 
81
- **〇Controller**__【viewで設定したhiddenの値を受け取ってCommnetテーブルの「post_id」カラムに格納したい】__
81
+ ****〇Controller**__**【viewで設定したhiddenの値を受け取ってCommnetテーブルの「post_id」カラムに格納したい】__
82
82
 
83
83
  public function add() {
84
84
 

3

誤字修正

2017/06/20 08:15

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -108,9 +108,9 @@
108
108
 
109
109
  こちらと同じ事がしたいのですがpost_idが「0」になってしまいます。
110
110
 
111
- **__http://www.grooving-earth.com/cakephp-lesson/#19
111
+ **__http://www.grooving-earth.com/cakephp-lesson/#19__**
112
112
 
113
- 19) コメント機能を実装__**
113
+ __19) コメント機能を実装__
114
114
 
115
115
 
116
116
 

2

URL追加

2017/06/20 08:13

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -104,6 +104,16 @@
104
104
 
105
105
  分からずずっと悩んでいます。
106
106
 
107
+
108
+
109
+ こちらと同じ事がしたいのですがpost_idが「0」になってしまいます。
110
+
111
+ **__http://www.grooving-earth.com/cakephp-lesson/#19
112
+
113
+ 19) コメント機能を実装__**
114
+
115
+
116
+
107
117
  変なコードを書いていると思いますがご教示いただきますと助かります。
108
118
 
109
119
  よろしくお願いいたします。

1

ソースの修正と質問内容を修正しました

2017/06/20 08:12

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,6 @@
1
1
  お世話になります。
2
+
3
+
2
4
 
3
5
 
4
6
 
@@ -24,15 +26,33 @@
24
26
 
25
27
  ◆つまづいている箇所
26
28
 
27
- ・PostというDBに「id」というカラムがあります。それぞれの記事に持たせている一意のIDです。
29
+ ・Postというテーブルに「id」というカラムがあります。
28
30
 
29
- こちらをコメント投稿をした際にhidden持たせコメントを登録するDB(Comment)テーブルの
31
+ Post.php(Model)で、$hasManyのforeignKeyに「**post_id**(Commentテーブルのカラム)」を設定し、
30
32
 
31
- post_id」というカラムに入れ、コメントがどの記事のものであるか区別をつけたいのですが、
33
+ **###Postテーブルのid = Commentテーブルのpost_id**
32
34
 
35
+ という状況になっているかと思います。
36
+
37
+
38
+
39
+ コメント投稿をした際にhiddenで「post_id」持たせ、コメントを登録するとCommentテーブルの
40
+
41
+ 「post_id」カラムに投稿した記事のid(Post)が登録され、どのコメントがどの記事のものであるか
42
+
43
+ 判別できるようにしたいのですが、
44
+
33
- 今現在、DBにコメントは登録されるものの、Commentテーブルの「post_id」は「0」になってしまい
45
+ 今現在、DBにコメントは登録されるものの、Commentテーブルの「post_id」は「0」になってしまい
34
46
 
35
47
  どのコメントがどの記事のものであるか分からない状態です。
48
+
49
+ 上記でも書いているように、
50
+
51
+ Post.php(Model)で、$hasManyのforeignKeyに「**post_id**(Commentテーブルのカラム)」を設定
52
+
53
+ しているのでViewで「echo $this->Form->hidden('Comment.post_id');」と書けば
54
+
55
+ Postテーブルのidがhiddenで送信出来ると思っているのですがそもそも間違っていますか?
36
56
 
37
57
 
38
58
 
@@ -40,19 +60,19 @@
40
60
 
41
61
 
42
62
 
43
- **〇view**__Postテーブルのカラム「id」をhiddenで持たせたい】__
63
+ **〇view**【hiddenでpost_idを持たせたい(Postテーブルのid)
44
64
 
45
- <?php
65
+ <?php
46
66
 
47
- echo $this->Form->create('Comment',array('url'=>array('controller' => 'comments', 'action' => 'add')));
67
+ echo $this->Form->create('Comment',array('url'=>array('controller' => 'comments', 'action' => 'add')));
48
68
 
49
- echo $this->Form->hidden('post_id',array('value' => $this->request->data('id')));
69
+ echo $this->Form->hidden('Comment.post_id');
50
70
 
51
- echo $this->Form->input('name',array('label'=>'名前'));
71
+ echo $this->Form->input('name',array('label'=>'名前'));
52
72
 
53
- echo $this->Form->input('comment',array('label'=>'コメント'));
73
+ echo $this->Form->input('comment',array('label'=>'コメント'));
54
74
 
55
- echo $this->Form->end('コメントを投稿する');
75
+ echo $this->Form->end('コメントを投稿する');
56
76
 
57
77
  ?>
58
78
 
@@ -62,23 +82,21 @@
62
82
 
63
83
  public function add() {
64
84
 
65
- if($this->request->is('post')){
85
+ if($this->request->is('post')){
66
86
 
67
- $id = $this->request->data('post_id');
87
+ if($this->Comment->save($this->request->data)){
68
88
 
69
- if($this->Comment->save($this->request->data($id)){
89
+ $this->Session->setFlash('投稿されました');
70
90
 
71
- $this->Session->setFlash('投稿されました');
72
-
73
- $this->redirect($this->referer());
91
+ $this->redirect($this->referer());
74
92
 
75
93
 
76
94
 
77
- }
95
+ }
78
96
 
79
- }
97
+ }
80
98
 
81
- }
99
+ }
82
100
 
83
101
 
84
102