我是Cakephp的新手,想知道如何使用分隔符保存多行。这是控制器:
if($this->data){
$array = explode(",",$this->data['Pincode']['pin1']);
foreach($array as $row){
$this->data['Pincode']['pin1'] = base64_encode($row);
$this->data['Pincode']['pin2'] = base64_encode($this->data['Pincode']['pin2']);
$this->data['Pincode']['add_date'] = time();
$this->Pincode->create();
if($this->Pincode->save($this->data) ){
$this->Session->setFlash('Saved', 'default', array('class' => 'success-msg'));
$this->redirect(array('controller' => 'managers', 'action' => 'pincodes?type=0'));
}
else{
$this->Session->setFlash('Error', 'default', array('class' => 'error-msg'));
}
}
}
当我尝试发布数据并保存时,它只保存第一行。 问题是foreach无法正常工作。 请帮帮我。感谢
答案 0 :(得分:1)
您可以尝试使用saveAll吗?
if (!empty($this->data)) {
$pins = explode(",", $this->data['Pincode']['pin1']);
$records = array();
foreach($pins as $pin) {
$record = $this->data;
$record['Pincode']['pin1'] = base64_encode($pin);
$record['Pincode']['pin2'] = base64_encode($record['pin2']);
$record['Pincode']['add_date'] = time();
$records[] = $record;
}
if ($this->Pincode->saveAll($records)) {
$this->Session->setFlash('Saved', 'default', array('class' => 'success-msg'));
$this->redirect(array('controller' => 'managers', 'action' => 'pincodes?type=0'));
}
else {
$this->Session->setFlash('Error', 'default', array('class' => 'error-msg'));
}
}