CakePHP2系で投稿機能で投稿した記事にコメントできる機能を実装しています。
DB上ではCommentテーブルのPost_idカラムに投稿した記事のPostテーブル上のPost_idと同じ値が入るようにしたいです。また、PostとCommentの二つのモデルは「public $hasMany = "Comment";」でリレーションが作れていると思うのですが、どうコードに落とし込んでいいか分からず、知恵をお借りしたいです。調べるとredirectに$idのパラメーターを乗せて送るみたいな方法もあるみたいなのですが(自信ないです)。
<?php class CommentsController extends AppController { //public $scaffold; public $helper = array('Html','Form'); public function comment($post_id) { if ($this->request->is('post')) { $data=$this->request->data; $id=$this->request->params['post_id']; $data['Comment']['post_id']=$post_id; if ($this->Comment->save($data)) { $this->Session->setFlash('Success!'); return $this->redirect('/comments/comment/'); //$this->Comment(array('controller' => 'comments', 'action' => 'view', $this->data['Comment']['id'])); } else { $this->Session->setFlash('failed'); } } } } ?> コード
ctp部分 <td> <?php echo $this->Html->link('Comment',array('controller'=>'comments', 'action'=>'comment', $post['Post']['id']))?> </td コード
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。