export()
アクションを使って
index.ctpで検索した検索結果をCSVファイルでダウンロードしたいです。
######今の状態
HTML
1<!--index.ctp--> 2<?= $this->Html->link(__('CSVダウンロード'), ['action' => 'export'],['class' => 'btn btn-block btn-default']) ?>
この「CSVダウンロード」ボタンをクリックした時
実行されるのが、以下のexport()
アクションです。
PHP
1//HogesController.php 2public function export() 3 { 4 $table = $this->Hoges->find('all'); 5 $_serialize = 'table'; 6 $_header = ['ID','名前','コメント']; 7 $_extract = ['id','name','comment']; 8 $this->response->download('sample.csv'); 9 $this->viewBuilder()->className('CsvView.Csv'); 10 $this->set(compact('table', '_serialize', '_header', '_extract')); 11 }
$this->Hoges->find('all')
で全件取得しているので
ここに検索結果の内容にすれば動作するかと考えています。
ただ、どうやって記述したらいいかわからず、、、
そもそもこの方法いいのか、、アドバイスいただければ幸いです。
回答2件
あなたの回答
tips
プレビュー