2016-04-21 59 views
0

下載我在CakePHP中framework.I新希望生成CSV file.Here我有結果數組,如:CSV文件沒有CakePHP的

Array 
(
[0] => Array 
    (
     [User] => Array 
      (
       [username] => yerica 
       [email] => [email protected] 
       [phone] => 993643636 
      ) 

     [DealPurchase] => Array 
      (
       [deal_title] => Tour de Zaragoza 
       [original_price] => 30 
      ) 

    ) 

[1] => Array 
    (
     [User] => Array 
      (
       [username] => Rama Test 
       [email] => [email protected] 
       [phone] => 9652369854 
      ) 

     [DealPurchase] => Array 
      (
       [deal_title] => Tour de Zaragoza 
       [original_price] => 30 
      ) 

    ) 

) 

在這裏,我要生成這個數組的CSV。

在這裏,我必須包括針對CsvHelper.php文件/傭工folder.This文件包含:

<?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文件e和功能看起來像:

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'); 

然後,它會給出一個error.So我怎樣才能解決這個問題呢?

注意:我有類似的錯誤:錯誤:在此服務器上找不到請求的地址'/ merchant/deal_list'。我已經在deal_list視圖文件中添加了下載csv鏈接。當我從助手中刪除Csv時,顯示此頁面。

錯誤:未定義的屬性:查看:: $ Csv。

+0

錯誤信息是什麼? –

+0

@JasonJoslin在Note中看到我更新的問題。 –

+0

您的商戶控制器中是否有deal_list()操作?它是什麼樣子的? –

回答

0

在這裏,我剛纔重命名CsvHelper.php幫手文件csv.php所以它的工作。那麼有誰能解釋爲什麼它的工作原因是因爲csv.php的名字?

+0

這是錯誤的。應該是CsvHelper.php。請參閱[手冊](http://book.cakephp.org/2.0/en/views/helpers.html#creating-helpers) – arilia

+0

@arilia是的,但現在它的工作,我不知道爲什麼。但是,當我使用CsvHelper.php時,它顯示一個錯誤,如**未定義的屬性:View :: $ Csv。** –

+0

您使用的是什麼版本的蛋糕? – arilia

0

視圖中的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); 
+0

我已經改變它在視圖文件,但它不工作。 –

+0

在幫助文件中,我必須更改?因爲我不知道它對我來說是新的。 –

+0

我甚至不知道它是否是你的問題的完整答案。只是注意到它是不正確的。 不在幫手中。在視圖中。看我的編輯。 –