我是 Cake 的新手,想知道如何在单个 saveall 函数中插入多行,我得到了这张表,
CREATE TABLE IF NOT EXISTS `dates` (
`date` varchar(10) COLLATE utf8_unicode_ci NOT NULL
)
我想要做的是让用户使用 JQuery calander 选择开始日期和结束日期,一旦提交此范围之间的所有日期将被保存到数据库中,我已经得到了日期数组,例如:
`array(
(int) 0 => '5/8/2013',
(int) 1 => '6/8/2013',
(int) 2 => '7/8/2013',
(int) 3 => '8/8/2013',
)
` 那么我的控制器看起来像这样:
public function index(){
if ($this->request->is('post')) {
$this->Date->create();
$data = array();
$data['dates']=array();
$startDate = $this->request->data['Date']['from'];
$endDate = $this->request->data['Date']['to'];
$datesBlocked = $this->loopDates($this->request->data['Date']['from'],$this->request->data['Date']['to']);
$data['dates'][] = $this->request->data['Blockdate']['from'];
$data['dates'][] = $this->request->data['Blockdate']['to'];
/*foreach($datesBlocked as $data) {
$data['dates'][] = $data;
}*/
if($this->Date->saveAll($data)) {
$this->Session->setFlash(__('done'));
if ($this->Session->read('UserAuth.User.user_group_id') == 1) {
// $this->redirect("/manages");
}
}
}
public function loopDates($from,$to){
$blockdates = array();
$start = strtotime($from);
$end = strtotime($to);
debug($start);
$counter = 0;
for($t=$start;$t<=$end;$t+=86400) {
$d = getdate($t);
$blockdates[$counter++] = $d['mday'].'/'.$d['mon'].'/'.$d['year'];
}
debug($blockdates);
return $blockdates;
}
问题是我无法让 foreach 工作,如果我取消注释 foreach,我收到错误说 Illegal string offset 'dates' ,所以我对此发表评论并尝试仅将开始日期和结束日期添加到数组中以查看是否有效,然后我得到另一个错误说。
`array(
'dates' => array(
(int) 0 => '08/05/2013',
(int) 1 => '09/05/2013'
)
) ` 注意(8):数组到字符串的转换[CORE\Cake\Model\Datasource\DboSource.php, line 1005]代码
因为我正在尝试在一个字段中插入 2 个值...我知道它应该像
`array(
'dates' => array( (int) 0 => '08/05/2013',
)
'dates' => array((int) 1 => '09/05/2013'
))
`但不知道该怎么做。任何帮助将不胜感激!!!!