質問編集履歴

2

Controller.php とctpファイルを編集しました。

2019/06/26 11:59

投稿

amaturePy
amaturePy

スコア131

test CHANGED
File without changes
test CHANGED
@@ -5,8 +5,6 @@
5
5
 
6
6
 
7
7
  ```
8
-
9
- CommentsController.php
10
8
 
11
9
  <?php
12
10
 
@@ -18,7 +16,7 @@
18
16
 
19
17
 
20
18
 
21
- public function comment()
19
+ public function comment($post_id)
22
20
 
23
21
  {
24
22
 
@@ -26,7 +24,15 @@
26
24
 
27
25
  {
28
26
 
27
+ $data=$this->request->data;
28
+
29
+ $id=$this->request->params['post_id'];
30
+
31
+ $data['Comment']['post_id']=$post_id;
32
+
33
+
34
+
29
- if ($this->Comment->save($this->request->data))
35
+ if ($this->Comment->save($data))
30
36
 
31
37
  {
32
38
 
@@ -58,73 +64,19 @@
58
64
 
59
65
  ```
60
66
 
61
- class PostsController extends AppController
67
+ ctp部分
62
68
 
63
- {
69
+
64
70
 
65
- public $hasMany = "Comment";
71
+ <td>
66
72
 
67
- public $presetVars = true;
73
+ <?php echo $this->Html->link('Comment',array('controller'=>'comments',
68
74
 
69
- public $uses = array('Post','User','Follow');
75
+ 'action'=>'comment',
70
76
 
71
- public $belongsTo = 'User';
77
+ $post['Post']['id']))?>
72
78
 
73
-
74
-
75
- //////////////////////////this is for comment functionality
76
-
77
- public function index()
78
-
79
- {
80
-
81
- $this->set('posts',$this->Post->find('all'));
82
-
83
- }
84
-
85
-
86
-
87
- public function view($id=null) {
88
-
89
- $this->Post->id = $id;
79
+ </td
90
-
91
- $this->set('post', $this->Post->read());
92
-
93
- }
94
-
95
-
96
-
97
- public function add()//////////投稿機能部分
98
-
99
- {
100
-
101
- $user_id=$this->Auth->user('id');
102
-
103
-
104
-
105
- if($this->request->is('post')) {
106
-
107
- $data=$this->request->data;
108
-
109
- $data['Post']['user_id']=$user_id;
110
-
111
- $this->Post->create($data);
112
-
113
-
114
-
115
- if($this->Post->save()) {
116
-
117
- $this->Flash->success(__('Your post has been saved'));
118
-
119
- return $this->redirect(array('controller'=>'Pages','action' => 'home'));
120
-
121
- }
122
-
123
- $this->Flash->error(__('Unable to add your post'));
124
-
125
- }
126
-
127
- }
128
80
 
129
81
 
130
82
 

1

質問文の誤字脱字修正しました。失礼いたしました。

2019/06/26 11:59

投稿

amaturePy
amaturePy

スコア131

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,6 @@
1
- CakePHP2系で投稿機能で投稿した記事にコメントできる機能を実装しているのでが、二つのモデルのは「public $hasMany = "Comment";」で作れていると思うのですが、どうコードに落とし込んでいいか分からず、知恵をお借りしたいです調べるとredirectに$idのパラメーターを乗せて送るみたいな方法もあるみたいなのですが(自信ないです)。
1
+ CakePHP2系で投稿機能で投稿した記事にコメントできる機能を実装していす。
2
+
3
+ DB上ではCommentテーブルのPost_idカラムに投稿した記事のPostテーブル上のPost_idと同じ値が入るようにしたいです。また、PostとCommentの二つのモデルは「public $hasMany = "Comment";」でリレーションが作れていると思うのですが、どうコードに落とし込んでいいか分からず、知恵をお借りしたいです。調べるとredirectに$idのパラメーターを乗せて送るみたいな方法もあるみたいなのですが(自信ないです)。
2
4
 
3
5
 
4
6