0
votes

I have two tables in the DB, cards and comments. I have my CakePHP app up and running. When on cards view.ctp .. the related comments appear at bottom of page.

I want to click add new comment but add it specifically for current card eg pre-populate card field to show current category.

This is my current view new comment link:

<?php

echo $this->Html->link(__('New comment'), array('controller' => 'comments','action' => 'add'));

?>

This is my add comment controller:

public function add() {
    if ($this->request->is('post')) {
        $this->Comment->create();
        if ($this->Comment->save($this->request->data)) {
            $this->Session->setFlash(__('The comment has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
        }
    }
    $cards = $this->Comment->Card->find('list');
    $this->set(compact('cards'));
}

How do I get the card to be set to current card for newly added comment?

I also want to be able to add new comment but with card blank.. ready for user to select if from list.

1

1 Answers

0
votes

You can attach the cartId to the link on the view page:

array(..., 'action'=>'add', $cartId)

then you can retrieve it as this:

public function add($cartId = null) {
    //...
    if (isPost) {
        //
    } else {
        $this->request->data['Comment']['cart_id'] = $cartId;
    }
    //...
}

this way the cartId will populate the select box on GET (on POST you want to use the posted param to ensure the form does remember its posted values in case of validation errors)