cakephp内でcsv出力する

  • debug=0にして変な出力をさせない
  • header()でtext/csv出力してやる
  • 強制的にexitして後処理とかさせない
    • 本当はよくないのかも
	function admin_export() {
		$params = array('order' => array('Customer.modified DESC'));
		Configure::write('debug', 0);
		
		$rows = $this->Customer->find('all', $params);
		header('Content-type: text/csv');
		header("Content-Disposition: attachment; filename=\"customer.csv\"");
		echo "id\tstatus\tname\tage\tmail\tadd1\tadd2\tadd3\ttel1\ttel2\tfax\r\n";
		foreach($rows as $row){
			echo $row['Customer']['id'] . "\t" .
				$row['Customer']['status'] . "\t" .
				$row['Customer']['name'] . "\t" .
				$row['Customer']['age'] . "\t" .
				$row['Customer']['mail'] . "\t" .
				$row['Customer']['add1'] . "\t" .
				$row['Customer']['add2'] . "\t" .
				$row['Customer']['add3'] . "\t" .
				$row['Customer']['tel1'] . "\t" .
				$row['Customer']['tel2'] . "\t" .
				$row['Customer']['fax'] . "\r\n";
		}
		exit;
		
	}