Tuesday, September 25, 2012

Re: passing ID from one controller to another

Well, if you're on a post page, and writing a comment there, don't you already know what the Post ID is?

You can just include the Post ID in the comment for, via a hidden field.

echo $this->Form->input('id', array('type' => 'hidden'));

On Tuesday, September 25, 2012 4:10:20 AM UTC-4, Chris wrote:
hi guys,... 
what I'm doing is creating comments for every blog post,...and I need to pass blog post ID to a comments table and having problem: Undefined variable id in CommentsComtroller.php 

this is my Model: 
<?php
class Comment extends AppModel {
   public $name = 'Comment';
   public $belongsTo = array('User', 'Post');
}
?>

and this is my CommentsController.php action 

<?php
class CommentsController extends AppController {
  var $name = 'Comments';

    public $uses = array('Post', 'Comment', 'User');
    public $helpers = array('Html', 'Form');
    public $components = array('Session');

public function isAuthorized($user) {

    if ($this->action === 'add') {
        return true;
    }

    return parent::isAuthorized($user);
}

    public function add() {

    if(!($post = $this->Post->findById($id)))

        if ($this->request->is('post')) {
        $this->request->data['Comment']['user_id'] = $this->Auth->user('id');

        $this->request->data['Comment']['post_id'] = $post['Post']['id'];  // HOW CAN I IDENTIFY POST ID FROM PostsController.php    ????? //

            $this->Comment->create();
            if ($this->Comment->save($this->request->data)) {
                $this->Session->setFlash('Your comment has been saved.');

                $this->redirect(array('controller' => 'posts', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Unable to add your post.');
            }
        }
    }


can anyone help...? 

thanks in advance 

--
Like Us on FacekBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
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.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: