我是cakephp框架的新手。我想生成csv文件。我有结果数组,如:
Array
(
[0] => Array
(
[User] => Array
(
[username] => yerica
[email] => amoreyerica@gmail.com
[phone] => 993643636
)
[DealPurchase] => Array
(
[deal_title] => Tour de Zaragoza
[original_price] => 30
)
)
[1] => Array
(
[User] => Array
(
[username] => Rama Test
[email] => rama@gmail.com
[phone] => 9652369854
)
[DealPurchase] => Array
(
[deal_title] => Tour de Zaragoza
[original_price] => 30
)
)
)
在这里,我想生成这个数组的csv。
在这里,我在view / helpers文件夹中包含了CsvHelper.php文件。该文件包含:
<?php
class CsvHelper extends AppHelper
{
var $delimiter = ',';
var $enclosure = '"';
var $filename = 'Export.csv';
var $line = array();
var $buffer;
function CsvHelper()
{
$this->clear();
}
function clear()
{
$this->line = array();
$this->buffer = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');
}
function addField($value)
{
$this->line[] = $value;
}
function endRow()
{
$this->addRow($this->line);
$this->line = array();
}
function addRow($row)
{
fputcsv($this->buffer, $row, $this->delimiter, $this->enclosure);
}
function renderHeaders()
{
header('Content-Type: text/csv');
header("Content-type:application/vnd.ms-excel");
header("Content-disposition:attachment;filename=".$this->filename);
}
function setFilename($filename)
{
$this->filename = $filename;
if (strtolower(substr($this->filename, -4)) != '.csv')
{
$this->filename .= '.csv';
}
}
function render($outputHeaders = true, $to_encoding = null, $from_encoding ="auto")
{
if ($outputHeaders)
{
if (is_string($outputHeaders))
{
$this->setFilename($outputHeaders);
}
$this->renderHeaders();
}
rewind($this->buffer);
$output = stream_get_contents($this->buffer);
if ($to_encoding)
{
$output = mb_convert_encoding($output, $to_encoding, $from_encoding);
}
return $this->output($output);
}
}
?>
在MerchantController(控制器)中,功能如下:
function download_csv($deal_id)
{
$options=array('fields'=>'User.username,User.email,User.phone,DealPurchase.deal_title,DealPurchase.original_price',
'joins' =>
array(
array(
'table' => 'deal_purchases',
'alias' => 'DealPurchase',
'type' => 'inner',
'foreignKey' => false,
'conditions'=> array('DealPurchase.user_id = User.id')
)));
$this->recursive = -1;
$data = $this->User->find('all', $options);
$this->set("users",$data);
$this->layout = null;
$this->autoLayout = false;
Configure::write('debug', '0');
}
在这里,我的视图文件代码是:
<?php
$line= $users[0]['User'];
$this->CSV->addRow(array_keys($line));
foreach ($users as $user)
{
$line = $user['User'];
$this->CSV->addRow($line);
}
$filename='users';
echo $this->CSV->render($filename);
这里,我的另一个包含下载csv文件和函数链接的函数如下:
function deal_list(){
$this->_checkMerchantSession();
$this->set('title_for_layout', 'Deal List');
$this->layout = "after_login";
//echo $this->Session->read('userData.Merchant.id');
//$conditions = "Deal.merchant_id ='".$this->Session->read('userData.Merchant.id')."' AND Deal.isdeleted ='0' AND Deal.isapproved ='0' ";
$conditions = "Deal.merchant_id ='".$this->Session->read('userData.Merchant.id')."' AND Deal.isdeleted ='0' ";
//$ArDealdetails = $this->Deal->find('all', array('conditions'=>$conditions,'order'=>'Deal.modified DESC'));
$this->paginate = array('conditions' => $conditions,'limit' =>5,'order'=>'Deal.id DESC');
$ArDealdetails = $this->paginate('Deal');
$this->set('ArDealdetails',$ArDealdetails);
$condition1 = "Merchant.id ='".$this->Session->read('userData.Merchant.id')."'";
$merchant_detail = $this->Merchant->find('first',array('conditions'=>$condition1));
$this->set('merchant_detail',$merchant_detail);
}
现在,当我将帮助器包含为Csv时,如:
var $helpers = array('Html', 'Form','Javascript','Fck','Js','Paginator','Csv');
然后它会出错。那么我该如何解决这个问题?
注意:我遇到如下错误:错误:在此服务器上找不到请求的地址'/ merchant / deal_list'。我添加了下载csv链接deal_list视图文件。当我从帮助者中删除 Csv 时,将显示此页面。
错误:未定义属性:查看:: $ Csv。
答案 0 :(得分:0)
在视图中csv帮助程序上的Casing错误。应为$this->Csv
<?php
$line= $users[0]['User'];
$this->Csv->addRow(array_keys($line));
foreach ($users as $user)
{
$line = $user['User'];
$this->Csv->addRow($line);
}
$filename='users';
echo $this->Csv->render($filename);
答案 1 :(得分:0)
这里我只是将 CsvHelper.php 帮助文件重命名为 csv.php ,以便它正常工作。所以有人可以解释为什么它的工作原因是因为 csv.php 这个名字?