tags:

views:

40

answers:

1

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->invalidFields();
    }
}
+2  A: 

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.

DavidYell
Thanks for the reply! I understand that I can access data like that in pre-existing records now, but how do I apply that when creating a new record? For example, I am creating a book review and I want to reference the book. Can I do function create($id = null) and then have the variable (like "1" in the URL /reviews/create/1/) reference the book_id with the id of "1"? Basically, how can I put the "1" in the URL into the database under book_id? And at the same time, pull the information from the book record id "1" and use it in the create view?
Justin
Seconded that it works well to decide upon your db schema, then baking models. Best part is that as you add fields to your schema, you can just start using those fields in code (just clear your model cache!)
Travis Leleu
If you are saving a record you do not need to worry about saving the related id. If your db is setup correctly and your model properly, it will save the id, from your selection of a dropdown box. Otherwise I'd use named parameters such as `/review/create/book:89` to pass stuff into the controller. Or you could just use `/review/create/89` and always assume that the last id is the book_id. Be sure to validate it though, and check it, or someone can add reviews to any book by changing the id.
DavidYell
Thanks again for the replies and working with me. After further research, is there anything I can do to "nest" things like: /books/1/review/create to send the book information into the review controller for that specific review? The reason I don't want to have it be a dropdown is because if a user clicks to review a book from the book information page, then they are putting in information twice. And if just passing it as a variable in the URL, how do you call that in the controller?
Justin
I would advise against this approach. You will find yourself with huge complex logic in your controllers, and you open up your application to people passing lots of odd parameters. I think you might be better off running a tutorial and learning about baking using the console. http://book.cakephp.org/view/1528/Blog
DavidYell
Ok, thanks for the help guys, it is much appreciated! I'm going to go with your suggestion and avoid passing the odd parameters.
Justin