Facing troubles baking associations in cakephp 2.5.5.12

74 views Asked by At

I am new to cakephp and fail to understand what i am doing wrong while baking . i use the cakephp/app path to bake my blog which has two tables :

posts Id-integer set to auto increment and primary key Title Body Created Modified

comments id-integer set to auto increment and primary key post_id name comment created modified

The associations i intend to have are posts hasMany comments and comments belongsTo posts All the Models bake successfully with the associations and validations . My comments add.ctp uses a drop down list and asks the user to select the post he wants to comment upon . I want the post to be set automatically without asking the user.Below is the snippet of my add action in commentscontroller.php

Add action

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.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
        }
    }
    $posts = $this->Comment->Post->find('list');
    $this->set(compact('posts'));
}

add.ctp(comments)

<div class="comments form">
<?php echo $this->Form->create('Comment'); ?>
<fieldset>
    <legend><?php echo __('Add Comment'); ?></legend>
<?php
    echo $this->Form->input('post_id');
    echo $this->Form->input('name');
    echo $this->Form->input('comment');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
    <li><?php echo $this->Html->link(__('List Comments'), array('action' => 'index')); ?></li>
    <li><?php echo $this->Html->link(__('List Posts'), array('controller' => 'posts', 'action' => 'index')); ?> </li>
    <li><?php echo $this->Html->link(__('New Post'), array('controller' => 'posts', 'action' => 'add')); ?> </li>
</ul>

1

There are 1 answers

2
Colonel Mustard On BEST ANSWER

I assume that you want to navigate from your post view to add a comment to the post. In that case, the button you would have the following form:

 <?php echo $this->Form->create('comment', array(
        'controller' => 'comment',
        'action' => 'add',
        'type' => 'get'
    ));
    echo $this->Form->input('id', array(
        'type' => 'hidden',
        'id' => 'id',
        'value' => $this->request->data['Post']['id']
    ));
    echo $this->Form->button('New Comment', array(
        'type' => 'submit',
        'class' => 'actionButton'
    ));
    echo $this->Form->end();

    ?>

This will pass the post ID via GET in the URL, and you can then have the following in the view:

<?php
            if (isset($this->request->query['id'])) {
                echo $this->Form->input('post_id', array(
                    'default' => $this->request->query['id']
                ));
            } else echo $this->Form->input('post_id', array(
                'empty' => '[select]'
            ));?>

This will then set the option for the post by default to the one sent in via GET