2016-07-01 23 views
1

我試圖將datetime變量傳入我的exportcsv方法中,以生成.csv文件,其中包含指定時間段的數據。CakePHP 3:在URL中傳遞數組時出錯

兩個變量都是關聯數組看起來像這樣:

[ 
'beginning' => [ 
    'year' => '2016', 
    'month' => '06', 
    'day' => '23', 
    'hour' => '11', 
    'minute' => '15' 
    ], 
    'end' => [ 
    'year' => '2016', 
    'month' => '06', 
    'day' => '29', 
    'hour' => '11', 
    'minute' => '15' 
    ] 
] 

當我試圖傳遞變量,我得到一個錯誤與此消息(是的,我有兩個相同的警告):

Warning (2): rawurlencode() expects parameter 1 to be string, array given [CORE\src\Routing\Route\Route.php, line 574] 

Warning (2): rawurlencode() expects parameter 1 to be string, array given [CORE\src\Routing\Route\Route.php, line 574] 

rawurlencode()是一個基本的PHP函數,這是line 574

$pass = implode('/', array_map('rawurlencode', $pass));

它看起來像URL重寫的一些問題,但坦率地說,我不知道如何解決它。有任何想法嗎?

exportcsv方法EventsController

public function exportcsv($beginning = null, $end = null) 
{ 
if ($this->request->is('post')) { 
    $beginning = $this->request->data['Export']['beginning']; 
    $end = $this->request->data['Export']['end'];    

    return $this->redirect([$beginning, $end]); 
} 

if (!$beginning && !$end) { 
    return $this->render(); 
} 

$this->response->download('export.csv'); 
$data = $this->Events->find('all') 
          ->select([ 
          'title', 
          'created', 
          'description', 
          'ended', 
          'working_hours', 
          'price', 
          'username' => 'Users.name', 
          'statusname' => 'Statuses.name', 
          'employeename' => 'Employees.name' 
          ]) 
          ->leftJoinWith('Statuses') 
          ->leftJoinWith('Users') 
          ->leftJoinWith('Employees') 
          ->where(["created BETWEEN " . $beginning . " AND " . $end]) 
          ->autoFields(true) 
          ->toArray(); 
$_serialize = 'data'; 
$_delimiter = chr(9); //tab 
$_extract = ['title', 'created', 'description', 'ended', 'working_hours', 'price', 'username', 'statusname', 'employeename']; 
$this->set(compact('data', '_serialize','_delimiter', '_extract')); 
$this->viewBuilder()->className('CsvView.Csv'); 
return; 
} 

exportcsv.ctp觀點:

<div class="events form large-6 medium-4 columns content"> 
    <?= $this->Form->create('Export'); ?> 
    <?= $this->Form->input('beginning', array('type'=>'datetime', 'interval' => 15, 'label' => 'Beginning:')); ?> 
    <?= $this->Form->input('end', array('type'=>'datetime', 'interval' => 15, 'label' => 'End:')); ?> 
    <?= $this->Form->button(__('Add')) ?> 
    <?= $this->Form->end() ?> 
</div> 
+0

其中是'rawurlencode()'或'線574'? – Poonam

+0

它看起來像'rawurlencode()'是一個基本的PHP函數。這是前面提到的一行:'$ pass = implode('/',array_map('rawurlencode',$ pass));' – PHPJunior

+0

爲什麼不在代碼中包含這一行?用這個代碼更新你的問題 – Poonam

回答

1

你不能傳遞數組的數組URL,路由器不支持。還需要另外而是通過,你的單值轉換爲正確的日期時間字符串,你可以很容易地做到這一點通過DateTimeType類,像

if ($this->request->is('post')) { 
    $beginning = $this->request->data('Export.beginning'); 
    $end = $this->request->data('Export.end'); 

    $type = \Cake\Database\Type::build('datetime'); 
    $beginning = $type->marshal($beginning)->format('Y-m-d H:i:s'); 
    $end = $type->marshal($end)->format('Y-m-d H:i:s'); 

    return $this->redirect([ 
     $beginning, 
     $end 
    ]); 
} 

而且,已經在評論中提到的,你需要修復您的where()調用,因爲它目前有一個SQL注入漏洞。 key => value項的鍵值以及僅值的項目不會被綁定,而是直接插入到查詢中!

蛋糕表達式生成器附帶的方法來安全地產生BETWEEN表達式:

->where(function(\Cake\Database\Expression\QueryExpression $exp) use ($beginning, $end) { 
    return $exp->between('Events.created', $beginning, $end); 
}); 

參見

0

您的重定向是錯誤

return $this->redirect([ 
    'controller' => 'yourController', 
    'action' => 'yourAction', 
    $yourData] 
); 

看到Cookbook更多信息,關於這個主題

+0

謝謝你的回覆。我試圖改變我的重定向到這樣的東西:'return $ this-> redirect(['controller'=>'Events','action'=>'exportcsv',$ beginning,$ end]);'但它給同樣的錯誤,當我嘗試這樣做:'return $ this-> redirect(['controller'=>'Events','action'=>'exportcsv',[$ beginning,$ end]]);'沒有任何反應。它沒有開始下載文件,只是重新加載頁面,所以我認爲值不是分配給'$ beginning'和'$ end'。 – PHPJunior