cakephp 1.2保存多个复选框
问题描述:
使用cakephp 1.2,我试图将复选框保存为字符串到数据库。然而,即使我存储的复选框作为一个字符串,它不节能。这里是视图:cakephp 1.2保存多个复选框
<?php echo $form->input('body_part',
array('label' => false,
'type' => 'select',
'multiple' => 'checkbox',
'options' => $bodyparts,
'name' => 'body_part',
'div' => false,
));
?>
这里是模型:
function beforeSave() {
if(($this->data->request['IrIncident']['body_part'])!=='') {
$this->data['IrIncident']['body_part'] = implode('|', $this->data['IrIncident']['body_part']);
$this->data['IrIncident']['rep_date'] = date('Y-m-d');
print_r($this->data);
}
}
这里是控制器:
function add($member_id = '') {
//var_dump($this->data);
//*Security* make sure user is logged in to access the add method
if ($this -> IrIncident -> loginCheck() == false) {
$this -> Session -> setFlash(__('Please login.', true));
$this -> redirect(array('action' => 'home', 'controller' => 'pages'));
exit();
}
$this->log($this->data);
if ($_SESSION['Incident']['employee'] != '') {
$memberid = $_SESSION['Incident']['employee'];
} else {
$this -> Session -> write('Incident.employee', $member_id);
}
if (!empty($this -> data)) {
$this -> IrIncident -> create();
if ($this -> IrIncident -> save($this -> data)) {
$this -> Session -> setFlash('Incident has been added.');
$this -> redirect(array('action' => 'index'));
}
}
//$this -> set('empref', $this -> IrIncident -> IrEmployee -> find('list', array('conditions' => array('emp_id' => $memberid))));
$empref = $this -> IrIncident -> IrEmployee -> find('list', array('fields' => array('id'), 'conditions' => array('IrEmployee.employee_number' => $memberid)));
$incDept = $this -> IrIncident -> IrGlobaldata -> find('list', array('fields' => array('dvalue1'), 'conditions' => array('IrGlobaldata.dtype' => 'Department'), 'order' => 'dvalue1 ASC'));
$incShift = $this -> IrIncident -> IrGlobaldata -> find('list', array('fields' => array('dvalue1'), 'conditions' => array('IrGlobaldata.dtype' => 'Shift')));
$incPosition = $this -> IrIncident -> IrGlobaldata -> find('list', array('fields' => array('dvalue1'), 'conditions' => array('IrGlobaldata.dtype' => 'Position'), 'order' => 'dvalue1 ASC'));
$incInjType = $this -> IrIncident -> IrGlobaldata -> find('list', array('fields' => array('dvalue1'), 'conditions' => array('IrGlobaldata.dtype' => 'Injury Type'), 'order' => 'dvalue1 ASC'));
$incNature = $this -> IrIncident -> IrGlobaldata -> find('list', array('fields' => array('dvalue1'), 'conditions' => array('IrGlobaldata.dtype' => 'Nature of Injury Damage'), 'order' => 'dvalue1 ASC'));
$incCause = $this -> IrIncident -> IrGlobaldata -> find('list', array('fields' => array('dvalue1'), 'conditions' => array('IrGlobaldata.dtype' => 'Cause of Injury'), 'order' => 'dvalue1 ASC'));
$incType = $this -> IrIncident -> IrGlobaldata -> find('list', array('fields' => array('dvalue1'), 'conditions' => array('IrGlobaldata.dtype' => 'Incident Type'), 'order' => 'dvalue1 ASC'));
$this -> set(compact('empref', 'incDept', 'incShift', 'incPosition', 'incInjType', 'incNature', 'incCause', 'incType'));
}
这里是所得到的数据:
Array ([IrIncident] => Array ([incident_type] => 4 [rep_date] => 2013-05-01 [date] => 2013-05-01 [emp_id] => 1 [department] => 8 [shift] => 10 [location] => Office Space [position] => 15 [injury_type] => 30 [nature_of_injury] => 39 [body_part] => eye, left|eye, right [description] => test [corrective_actions] => test [immediate_cause] => test [basic_root_cause] => test [mod_duty] => Y [mod_days] => 10 [est_cost] => 500 [cause_of_injury] => 56 [responsibility] => You [lost_shifts] => 10 [target_date] => 2013-05-01 [comp_date] =>))
这就是我所拥有的,以及所有的领域(除了comp_date,这在数据库或cakePHP验证中不需要),它仍然不能保存。问题是复选框。不知道为什么。
答
没关系,正如我注意到的,我在代码的末尾没有return true
。
代码:
function beforeSave() {
if(($this->data->request['IrIncident']['body_part'])!=='') {
$this->data['IrIncident']['body_part'] = implode('|', $this->data['IrIncident']['body_part']);
$this->data['IrIncident']['rep_date'] = date('Y-m-d');
}
return true;
}