我是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。
在这里,我在视图/助手文件夹中包含 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);
}
}
?>
在商家控制器(控制器)函数中看起来像:
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');
然后它会给出一个错误。那么我该如何解决这个问题呢?
注意:我有一个错误,例如:错误:在此服务器上找不到请求的地址"/商家/deal_list"。我在视图文件中添加了下载 csv 链接deal_list。当我从帮助程序中删除 Csv 时,将显示此页面。
错误:未定义的属性:查看::$Csv。
视图中的
csv 助手的大小写错误。应该$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);
在这里我刚刚将 CsvHelper.php 助手文件重命名为 csv.php所以它可以工作。那么谁能解释一下为什么它因为 csv .php名称而工作?