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>
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:
This will pass the post ID via GET in the URL, and you can then have the following in the view:
This will then set the option for the post by default to the one sent in via GET