いろんなやり方があるだろうけど、以下ざっと覚え書き。
コントローラ
1 2 3 4 5 6 7 |
public function export() { $products = $this->Product->find('all', array( 'recursive' => -1, )); $this->set(compact('products')); $this->layout = false; } |
ビュー
1 2 3 4 5 6 7 8 9 10 11 |
<?php header('Content-Type: application/csv'); header('Content-Disposition: attachment; filename=products-' . date('YmdHis') . '.csv'); header('Pragma: no-cache'); ?> <?php foreach ($products[0]['Product'] as $key => $value): ?>"<?php echo $key; ?>",<?php endforeach; ?> <?php echo "n"; ?> <?php foreach ($products as $product): ?> <?php foreach ($product['Product'] as $key => $value): ?>"<?php echo h($value); ?>",<?php endforeach; ?> <?php echo "n"; ?> <?php endforeach; ?> |