2012-05-08 5 views
0

두 테이블과 조인 테이블이 있습니다. 나는 $ hasAndBelongsToMany를 사용하여 시도했지만 작업을하지 못했습니다. 그래서 대신 조인 테이블의 모델을 만들고 사용했습니다.cakephp HABTM association saveAll?

User hasMany Membership 
Membership belongsTo User, Club 
Club hasMany Membership. 

두 테이블 모두에 저장되는 양식이 있습니다.

function dashboard_add(){ 

     $user = $this->Session->read('User'); 

     $register = false; 

     if (!empty($this->data)) { 



       if($this->Club->saveAll($this->data)){ 
        $this->Session->setFlash(__('You have registered your club. You will be contacted soon!', true), 'default', array('class' => 'success')); 
        $this->redirect(array('action' => 'index')); 
       } else { 
        $this->Session->setFlash(__('There was a problem with your registration. Please, try again.', true), 'default', array('class' => 'warning')); 
       } 

     } 


     if (empty($this->data)) { 
      $this->data = $this->User->read(null, $user['User']['id']); 
     } 

     $this->set(compact('register')); 

    } 

사용자 모델이

var $hasMany = array(
     'Membership' => array(
      'className' => 'Membership', 
      'foreignKey' => 'user_id' 
     ) 
    ); 

이있는 회원 모델

var $belongsTo = array('User','Membership'); 

을 가지고 클럽 모델은 내가 오류를 얻을하지 않습니다

var $hasMany = array(
     'Membership' => array(
      'className' => 'Membership', 
      'foreignKey' => 'club_id' 
     ), 
     'Upload' => array(
      'className' => 'Upload', 
      'foreignKey' => 'club_id' 
     ) 
    ); 

있습니다. 클럽 테이블이 채워지지만 회원 및 사용자 테이블에 삽입/업데이트되지 않았습니다.

업데이트 : 디버그 ($ this-> Club-> save ($ this-> data));

Array 
(
    [User] => Array 
     (
      [id] => 1 
      [first_name] => this 
      [last_name] => that 
      [company_name] => other 
      [city] => this 
      [state] => that 
      [zip] => other 
      [telephone] => that 
      [email_address] => this 
     ) 

    [Club] => Array 
     (
      [address] => fvdfvx 
      [title] => xcvxcv 
      [category] => xcvxcv 
      [modified] => 2012-05-03 06:31:12 
     ) 

) 

답변

0

난 당신이 CakePHP의 1.3.x에를 사용하는 가정 케이크가 동시에 많은 모델을 저장하는 방법을 이해하기 위해 설명서를 참조하십시오, 당신은 예를 들어 .. 저장하는 모든 모델 배열을 생성해야합니다

Array 
(
    [Article] => Array 
     (
      [title] => My first article 
     ) 
    [Comment] => Array 
     (
      [0] => Array 
       (
        [comment] => Comment 1 
      [user_id] => 1 
       ) 
     [1] => Array 
       (
        [comment] => Comment 2 
      [user_id] => 2 
       ) 
     ) 
) 

http://book.cakephp.org/1.3/view/1031/Saving-Your-Data

분명히 this- 당신의 $에> 데이터는 간단한 배열 [사용자] 만 있습니다. 예제와 같이 저장할 여러 배열이 아닙니다.

도움이 되었기를 바랍니다. 감사합니다.

+0

설명서를 읽습니다. saveAll 직후 $ this-> data 배열로 내 질문을 업데이트했습니다. – madphp

+0

수정. 나는 문서화와 같이 save()로 변경했다. – madphp