0

Trying to save the current event id '14' in the url '/localhost/topp/events/view/14' to a event_id field in a comments table. I am using the add function in the comments controller and an 'add comment' button on the events view.ctp page. Any ideas? I have been advised to use a hidden id field and $this->params['pass']['0'] but not sure how to go about this, any advice is welcome. Thanks in advance.

My current comments controller add action code:

public function add() {
        //$this->loadModel('Event');
        if ($this->request->is('post')) {
            $this->Comment->create();
            $this->request->data['Comment']['user_id'] = $this->Auth->user('id');
                    //Commented below is the logic Iam trying to achieve but is wrong
            //$this->request->data['Comment']['event_id'] = $this->request->data['Event']['id'];
            if ($this->Comment->save($this->request->data)) {
                $this->Session->setFlash(__('The comment has been saved'));
                $this->redirect(array('controller' => 'events', 'action' => 'myevents'));
            } else {
                $this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
            }
        }
    }

//////////////////////////////////////// My Events View code with add comment link//////////////////////////

<div class="events view">
    <?php echo $this->element('maintitle'); ?>
    <?php echo $this->element('profile_area'); ?>
<h2><?php  echo __('Event'); ?></h2>
    <dl>        
        <td><?php echo $this->Html->image('/files/event/photo/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo'], array("alt" => "No Event Image")); ?>&nbsp;</td>
        <td><?php echo $this->Html->image('/files/event/photo2/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo2'], array("alt" => "No Event Image")); ?>&nbsp;</td>
        <td><?php echo $this->Html->image('/files/event/photo3/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo3'], array("alt" => "No Event Image")); ?>&nbsp;</td>
        <td><?php echo $this->Html->image('/files/event/photo4/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo4'], array("alt" => "No Event Image")); ?>&nbsp;</td>
        <dt></dt>
        <dd>
        <br>
        </dd>
        <dt><?php echo __('Name'); ?></dt>
        <dd>
            <?php echo h($event['Event']['name']); ?>
            &nbsp;
        </dd>
        <dt><?php echo __('Date'); ?></dt>
        <dd>
            <?php echo h($event['Event']['date']); ?>
            &nbsp;
        </dd>


    </dl>
</div>
<div class="related">
    <h3><?php echo __('Related Comments'); ?></h3>
    <?php if (!empty($event['Comment'])): ?>

    <?php echo ('Add a comment for this event') ?>
    <?php
        $i = 0;
        foreach ($event['Comment'] as $comment): ?>

        <tr>
            <td><?php echo $comment['comment']; ?></td>

            <td class="actions">

            </td>
        </tr>
    <?php endforeach; ?>

<?php endif; ?>

    <div class="actions">
        <ul>
            <li><?php echo $this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add')); ?> </li>
        </ul>
    </div>
</div>
Joshua
  • 371
  • 2
  • 6
  • 23
  • Post the view code of the Event :) Just the part where you have the form that adds the comment – Nunser Jun 18 '13 at 19:35

1 Answers1

1

Let me see if I got this right.

  1. The form you posted is in the event view
  2. The link "New Comment" redirects to another view "add", in the comments controller
  3. This add view has a form to actually write a comment (I'm guessing this part, you didn't put that code here)
  4. You want the add function to have the event_id from where this came from.

Right?

You then need to pass the id of the event from event_view.ctp (I'm inventing names here) to comment_add.ctp.

So two ways to do it

First way: pass the id of the event in the link and recieve it in the action like this

//event_view.ctp
//I'm going to assume you know the event id here
$this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add', $event_id)); ?>

That will create a link like comments/add/14, for example, where 14 is the event id. Now in the action, you receive the id

//pass it like parameter of the action
public function add($event_id = null) {
    if ($this->request->is('post')) {
        $this->Comment->create();
        $this->request->data['Comment']['user_id'] = $this->Auth->user('id');

        //check that the event_id isn't null and that the event actually exists first
        $this->request->data['Comment']['event_id'] = $event_id;
        /*etc*/
    }
}

You don't need to pass the event_id to the comment_add view, if the user doesn't need to handle it, there's no need to add it to the form.

Second way: If you don't want to pass the event_id inside the url, you need to pass is by POST. To do that, the link you now have to add new comments needs to change to a form with the event_id hidden.

echo $this->Form->create('Comment', array('url'=>array('controller' => 'comments', 'action' => 'add')));
echo $this->Form->input('event_id', array('type'=>'hidden', 'value'=>$event_id);
echo $this->Form->end('New comment');

//instead of <?php echo $this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add')); ?>

And in the action of the comments, check if the post you receive contains event_id, but not user_id, for example. I find this way a bit messy because every request you receive for that action is going to be a post, so it's be harder to differentiate when you want to display the form to be filled, and when you want to save it.

I like the first way better, and I recommend doing that. But I felt the need to point out there are other ways to do it. Depends what you want.

Nunser
  • 4,512
  • 8
  • 25
  • 37
  • 1
    Nunser your a genius, thanks for your detailed and in-depth response to this question. I implemented the first method and it works perfectly. I have learnt alot from this answer thanks again bro. :) – Joshua Jun 20 '13 at 18:24