Thursday, June 25, 2009

Maintaining association state in views

Been a while since I've done some cake (and last app was a
time-crunched blur) so thought I'd start fresh, from the beginning,
with a more relaxed deadline and get some fundamentals down correctly.

Using the blog example, our models look like this:

Post hasMany Comment
Comment belongsTo Post

Let's say I want to add a new comment -- in the "add" view of
Comments.add action, I can create a hidden form field with the Post
Id; when I call save, the foreign key (post_id) is set for the Comment
and then saved accordingly, preserving the relationship between Post
and Comment.

Because I want to keep the relationship in context to the user -- I
don't want them to go from /posts/view/1 to /comments/add, only to
have them choose a Post they want to comment on from a select field --
it seems like I have two options:

1) include the comment form on the post view, using an element perhaps.
2) pass the Post Id as an argument to the Comment.add action; save and
redirect back to posts/view/1.

#1 is fine for a blog, but not for a User's Profile, which could be
too long to include in the User view.
#2 works I guess, but adding Post-specific view and redirect logic to
the CommentsController actions doesn't seem right, when (I think) I
should be trying to maintain a 1:1 relationship between controllers
and models.

Any thoughts?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: