使用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,这在DB或cakePHP验证中不是必需的),它仍然没有保存。问题是复选框。不知道为什么。
答案 0 :(得分:0)
没关系,正如我注意到的那样,我在代码末尾没有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;
}