I have been searching for a couple hours for a way to accomplish this task (which I know is fairly elementary), but I can't find any documentation...
I have three models - reviews, users, and books - and I'm designing the review controller and add action for that controller. I already have the user_id pulled from the session and used to populate the review to be "written by that user." However, what is the best way to add in the book that is being reviewed? Also, is there a way to populate the "create" page with book information from the book model?
I'm thinking of having the URL be /reviews/1/ format, where 1 is the book_id, but having create($id = null), but how do I translate that into a read command and integrate it into the data variable?
Any help would be greatly appreciated!
EDIT: This is how my controller looks now:
function create() {
if($this->Review->create($this->data) && $this->Review->validates()) {
$this->data['Review']['user_id'] = $this->Session->read('Auth.User.id');
$this->Review->save($this->data);
$this->redirect(array('action' => 'index'));
} else {
$errors = $this->Review->invalidFi开发者_运维百科elds();
}
}
Sounds to me like you are trying to achieve something that Cake does by default. If you have your model associations setup correctly, when creating a review of a book, you should have the book information available already in $this->data['Book']
Have a look at your models and ensure that they are linked together using proper relationships. http://book.cakephp.org/view/1039/Associations-Linking-Models-Together
I tend to setup my database and have cake bake my models for me, as it saves time.
精彩评论