두 테이블과 조인 테이블이 있습니다. 나는 $ 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
)
)
설명서를 읽습니다. saveAll 직후 $ this-> data 배열로 내 질문을 업데이트했습니다. – madphp
수정. 나는 문서화와 같이 save()로 변경했다. – madphp