###やりたいこと
Todoリストを作成しており、トップ画面でTodo一覧を表示しております。
この検索機能を実装しているのですが、うまくいきません。
概要
index上の「検索」押下→searchアクション呼び出し→Controllerのsearch()→indexにリダイレクト
ctp
1<!--index.ctp--> 2 <h1>Todoリスト</h1> 3 4 <h2>検索</h2> 5 6 <fieldset> 7 <?php 8 echo $this->Form->create('find', [ 9 'type' => 'get', 10 'url' => [ 11 'controller' => 'todo', 12 'action' => 'search' 13 ] 14 ]); 15 16 echo $this->Form->input( 17 'f_task_name', 18 [ 19 'type' => 'text', 20 'label' => 'タスク名を入力' 21 ] 22 ); 23 echo $this->Form->button('検索'); 24 echo $this->Form->end() 25 ?> 26 </fieldset> 27 28 <h2>一覧</h2> 29 30 <?= $this->Html->link('新規追加', ['action' => 'new_task']) ?> 31 32 <table> 33 <th>タスク名</th> 34 <th>詳細</th> 35 <th>担当者</th> 36 <th>状態</th> 37 <th>締め切り</th> 38 <th>登録日</th> 39 <th>更新日</th> 40 41 <?php foreach ($tasks as $task): ?> 42 <tr> 43 <td><?= $this->Html->link($task->task_name, ['action' => 'view', $task->task_id]) ?></td> 44 <td><?= h($task->task_detail); ?></td> 45 <td><?= h($task->fk_user_id); ?></td> 46 <td><?= h($task->fk_status_id); ?></td> 47 <td><?= h($task->deadline); ?></td> 48 <td><?= h($task->create_date); ?></td> 49 <td><?= h($task->update_date); ?></td> 50 </tr> 51 <?php endforeach; ?> 52 </table>
php
1 2//TodoController.php 3 public function index() { 4 5 $tasks = []; 6 7 $tasks = $this->Tasks->find() 8 ->where(['del_flg' => '0']); 9 10 $this->set(compact('tasks')); 11 12 } //end index() 13 14 15 public function search(){ 16 17 $f_task_name = $this->request->data; 18 19 $this->set([ 20 'f_task_name' => $f_task_name 21 ]); 22 23 24 if($this->request->is('get')){ 25 26 $tasks = $this->Tasks->find('all', [ 27 'conditions' => [ 28 'task_name LIKE' => '%'.$f_task_name.'%' 29 ] 30 ]); 31 32 $this->set(compact('tasks')); 33 34 return $this->redirect('/todo/index'); 35 36 } 37 38 } //end search() 39 40
タスク名で検索するとすべてのタスクが表示されてしまいます。
エラーはなく、そのままindexに戻ります。
ご都合のよろしい方、ご教授願います。
どうぞ、よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/09 03:06