2013-05-01 70 views
0

使用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驗證中不需要),它仍然不能保存。問題是複選框。不知道爲什麼。

回答

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; 
}