我試圖將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>
其中是'rawurlencode()'或'線574'? – Poonam
它看起來像'rawurlencode()'是一個基本的PHP函數。這是前面提到的一行:'$ pass = implode('/',array_map('rawurlencode',$ pass));' – PHPJunior
爲什麼不在代碼中包含這一行?用這個代碼更新你的問題 – Poonam