0

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] => ) )

それが私が持っているものであり、すべてのフィールド (DB または CakePHP の検証では必要ない comp_date を除く) であり、まだ保存されていません。問題はチェックボックスです。理由がわからない。

4

1 に答える 1

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;
}
于 2013-05-01T16:06:07.380 に答える