Reputation: 376
I need help with creating the right relations between 3 tables in CakePHP. These are:
I am trying to make my users_books-table contain all the relations between what book belongs to what user, and vica versa, like:
ID | user_id | book_id
--------------------------
1 321 231
2 24 58
3 80 58
4 24 75
5 80 231
My approach has been to make a hasAndBelongsToMany-relation, but when adding a new book the users_books-table does not get populated with any info.
In my controller I've tried the saveAll() and saveAssociated()-method but nothing works.
Can anybody help me with what association-code i should put in my Models to make this work?
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.'));
}
}
The the new array:
$test = array('User' => $this->Auth->user('user_id'), 'Book' => $data);
pr($test);
Outputs this:
Array
(
[User] => 4
[Book] => Array
(
[book_study_id] => ha_fil
[book_title] => The title
[book_price] => 123
[book_isbn] => 123
[book_user_id] => 4
)
)
Thanks in advance, Jesper.
Upvotes: 0
Views: 40
Reputation: 4522
You are quite confused with HABTM...
Please refer to the docs when you are in doubt.
In there you have an example of how the saved array should be
Array
(
[Recipe] => Array
(
[id] => 42
)
[Tag] => Array
(
[name] => Italian
)
)
So, transforming that to what you want to do, you need to save this array...
Array
(
[User] => Array
(
[id] => $this->Auth->user('user_id')
)
[Book] => Array
(
[name] => book,
/* your other book stuff */
)
)
and do a simple $this->save($suggestedBySOArray)
to save the relation.
The link to the docs have more examples on how to save data to HABTM if you need more examples.
EDIT:
I'll add a bit more code to help you get on the right path. Now, I'm assuming things* in this example since your question doesn't say:
1) you are creating a new book, not updating a relation between an existing user with and existing book.
2) you are just adding one book, not many
$this->Book->create();
//what did you intent to do with this piece of code btw? you don't have a "book_user_id" column anywhere
//$this->request->data['Book']['book_user_id'] = $this->Auth->user('user_id');
$data = $this->request->data;
if (!$data['Book']['book_img']['name']) {
unset($data['Book']['book_img']);
}
$data['User']['id'] = $this->Auth->user('user_id');
if ($this->Book->save($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.'));
}
That ought to do it.
Now, please, read the docs for anything further, it's got examples and everything.
Also, if what @Anilkumar suspected what correct ("You're trying to add book for only one user"), go with hasMany instead of HABTM.
Upvotes: 1