tags:

views:

174

answers:

2

I'm building off the CakePHP tutorial for the blog engine by adding comments to each post. I am able to add comments by selecting the post that it should be attached to, via a select box. I would like to be able to click an "Add Comment" link within the post and have the association to the post formed programatically. I am unsure how I can pass the post_id to the add method within my comments_controller. The body of my add method is the auto-generated scaffold code. Is it as easy as adding a $postId argument to the add method and write this to the post_id in my comments model? This doesn't feel right though, since I would expect add to be called when my submit button is click on my comments add view.

Thanks all.

EDIT - Added the code that I'm working with currently. It is just the add method in my comments_controller.

function add($postid = null) {             
    if(!empty($this->data) {
         $this->Comment->create();                          
         $this->Comment->post_id = $postid;
         if ($this->Comment->save($this->data)) {
            $this->Session->setFlash(__('The Comment has been saved', true));
            $this->redirect(array('action' => 'index'));
         } else {
             $this->Session->setFlash(__('The Comment could not be saved. Please, 
                                          try again.', true));
         }
    }   
    $this->set('post_id', $postid);     
    print_r($postid);
}
A: 

Create your link at the bottom of your blog post as,

<?php echo $html->link('Add Comment', array('controller'=>'Comments','action'=>'add',$post->id)) ?>

Then you can, in your Comments controller's Add method,

function add($postid){
  $this->data->Comment->post_id = $postid;
  $this->data->Save();
}

Similar to this would do you just fine I'd say. Then your url would be example.com/comments/add/3 Double check the code though, as it's first thing in the morning and we've run out of milk, so I have had no coffee! ;)

DavidYell
Thanks for the push! The correct line of code should read:$this->Comment->set('post_id', $postid);
Ryan
Only if you are pushing it from your COmments controller out to your view.When dealing with posted data in your controller, it's all accessible from $this->data so you can just overwrite values directly if need be.
DavidYell
I was a bit premature in thinking I had fixed the problem. I was able to store values using both the method you recommend and through a couple other ways IF i am writing out a string ($this->Comment->post_id ='1';). If i use the $postid variable a 0 gets written to the post_id field of my comments table entry. Any thoughts? I can use print_r and see that $postid is what I've passed in to the add function. Any thoughts?
Ryan
Where are you doing your `print_r()` also you can use `pr()` Cake shorthand instead. Sounds like your variable isn't being passed
DavidYell
I've edited my original post to show my current code. I'm doing my print_r() as the last line of my add method. Does add get called more than once? Once when I goto my add page and once again when I post my form data?
Ryan
+1  A: 
function add($postid = null) {             
    if(!empty($this->data) {
         $this->Comment->create();                          
         $this->data['Comment']['post_id'] = $postid; // see how it needs to be?
         ...then save the data...
Sergei
This is what I meant! ;)
DavidYell
When I do that, warning 512 is thrown "SQL Error: 1048: Column 'post_id' cannot be null".
Ryan
you kidding? of course you MUST provide $post_id = <integer>.
Sergei
I am not kidding. I add a variable $post_id and set it = <int> and it will write out that value in my table, but as soon as I switch back to the variable (or set $post_id=$postid) I get the warning 512 again. I've been fighting this for a week now and have NO idea what I am going wrong or I do not understand about the framework.
Ryan
you should set post_id either from url (/comments/add/post_id/) or through the hidden field in comment add form: $form->input('post_id',array('type'=>'hidden')) and then you dont need the row $this->data['Comment']['post_id'] = $postid;
Sergei