Cakephpを学習中です。
簡単な掲示板を作っています。
削除ボタンを実装し(各テーブルに)、押下時に指定のレコードが削除される。
という実装を行いたいのですが、
削除ボタンを押下すると、errorが出てしまいます。
そして、indexに戻るとレコードが全て消えてしまいます。
どのように実装すればよいでしょうか。
html
1<h1>Sample BBS</h1> 2 3<?=$this->Form->create($entity,['url'=>['action'=>'addRecord']])?> 4<fieldset> 5 name:<?=$this->Form->text("user_name");?> 6 title:<?=$this->Form->text("title");?> 7 content:<?=$this->Form->text("content");?> 8</fieldset> 9 <?=$this->Form->button('投稿する');?> 10 <?=$this->Form->end();?> 11 12<table> 13 <thead> 14 <tr> 15 <th>Id</th> 16 <th>name</th> 17 <th>title</th> 18 <th>content</th> 19 <th>delete</th> 20 </tr> 21 </thead> 22 23 <tbody> 24 <?php foreach($data as $obj):?> 25 <tr> 26 <td><?=h($obj->id)?></td> 27 <td><?=h($obj->user_name)?></td> 28 <td><?=h($obj->title)?></td> 29 <td><?=h($obj->content)?></td> 30 <!--削除する--> 31 <td><?=$this->Form->postLink('削除',array('action'=>'delRecord'));?></td> 32 </tr> 33 <?php endforeach;?> 34 </tbody> 35</table> 36
PHP
1<?php 2namespace App\Controller; 3use \Exception; 4use Cake\log\log; 5 6class PostsController extends AppController{ 7 //index action 8 public function index(){ 9 $data = $this->Posts->find('all'); 10 $this->set('data',$data); 11 $this->set('entity',$this->Posts->newEntity()); 12 } 13 14 //レコードの追加 15 public function addRecord(){ 16 if($this->request->is('post')){ 17 $p = $this->Posts->newEntity($this->request->data); 18 $this->Posts->save($p); 19 } 20 return $this->redirect(['action'=>'index']); 21 } 22 23 //レコードの削除 24 public function delRecord(){ 25 if($this->request->is('post')){ 26 $this->Posts->deleteAll(); 27 } 28 return $this->redirect(['action'=>'index']); 29 } 30} 31
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。