CakePHP에서 3 개의 테이블 사이의 올바른 관계를 생성하는 데 도움이 필요합니다. 이들은 :CakePHP의 테이블 간의 관계
- 사용자 - PK = user_id를
- 책 - PK = book_id
- books_users
나는이 책이 소속 된 사이에 내 users_books 테이블의 모든 관계를 포함하기 위해 노력하고 어떤 사용자, 그리고 그 반대의 경우 :
ID | user_id | book_id
--------------------------
1 321 231
2 24 58
3 80 58
4 24 75
5 80 231
내 접근 방식은 hasAndBelongsToMany-relation, bu 새 책을 추가 할 때 users_books-table에는 정보가 채워지지 않습니다.
내 컨트롤러에서 saveAll() 및 saveAssociated() - 메서드를 시도했지만 아무 것도 작동하지 않습니다.
아무도 내가이 모델을 작성하기 위해 모델에 넣어야 할 연관성 코드를 알려줄 수 있습니까?
public function addBook() {
$this->Book->create();
$this->request->data['Book']['book_user_id'] = $this->Auth->user('user_id');
$data = $this->request->data['Book'];
if (!$data['book_img']['name']) {
unset($data['book_img']);
}
if ($this->Book->saveAll($data)) {
$this->Session->setFlash(__('The book has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The book could not be saved. Please, try again.'));
}
}
다음은 새로운 배열 :
$test = array('User' => $this->Auth->user('user_id'), 'Book' => $data);
pr($test);
출력이 :
Array
(
[User] => 4
[Book] => Array
(
[book_study_id] => ha_fil
[book_title] => The title
[book_price] => 123
[book_isbn] => 123
[book_user_id] => 4
)
) 사전에
감사합니다, 예스퍼. 당신이 의심 스러울 때 HABTM 꽤 혼란
또한'view'와'controller' 코드를 넣으십시오. –
@Anilkumar 컨트롤러 동작이 어떻게 보이는지 추가했는데, 내보기가 어떻게 보이는지 관련이 있습니까? –
[Docs] (http://book.cakephp.org/2.0/ko/models/associations-linking-models-together.html#hasandbelongstomany-habtm) ... ** 표 이름은 규칙에 따라 사전 순으로 * . 연관 정의에서 사용자 정의 테이블 이름을 정의 할 수 있습니다. ** – Nunser