2010-01-06 5 views

답변

5

붙여 넣기 (즉, 뷰/사용자/add.ctp) 사용자의 컨트롤러에

<?php echo $form->create('User', array('action' => 'add')) ?> 
<?php echo $form->input('User.hobbies', array('type' => 'select', 
               'multiple' => 'checkbox', 
               'options' => array('sports' => 'sports', 
                   'movies' => 'movies', 
                   'games' => 'games'))) ?> 
<?php echo $form->end('Save') ?> 

붙여 넣기 (저장 방법 단지 표준, 여기에 특별한 아무것도) 사용자에

function add() { 
    if(!empty($this->data)) { 
    if($this->User->saveAll($this->data, array('validate' => 'first'))) { 
     $this->Session->setFlash('User saved successfully'); 
     } else { 
     $this->Session->setFlash('User failed to save'); 
     } 
    } 
} 

붙여 넣기 모델

function beforeValidate() { 
    // join hobbies into csv 
    if(!empty($this->data['User']['hobbies'])) { 
     $this->data['User']['hobbies'] = join(',', $this->data['User']['hobbies']); 
    } 

    return true; 
} 

주 :

  • 사용자 모델을 읽을 때 취미를 분리해야하는 경우 "afterFind"콜백을 사용하거나 배열 추가 또는 제거를 시도 할 때마다 자동으로 직렬화 및 비 직렬화되는 Serializable Behavior http://blog.matsimitsu.nl/code/206/serializeable-behavior-for-cakephp을 확인할 수 있습니다./DB에서.
  • 대신 beforeSave 콜백에 beforeValidate 코드를 추가 할 수 있습니다. 수행하려는 유효성 검사의 종류에 따라 달라집니다. beforeValidate에 코드가 있으면 기본 notEmpty 검사를 수행 할 수 있지만 beforeSave에서는 배열에 개별 항목이 있는지 확인할 수 있습니다.

참고 :

관련 문제