Trouble with CakePHP Hidden Tag
I started using CakePHP for a web project that I am doing because I want to update my web development skills. I am making a simple blog site to start off. I have a Posts model, a Comments model, and a Tags model. I am leaving the Tags model alone and focusing on getting Comments going. I am running up against a problem having to do with a Posts model and the HasMany relationship with the Comments model.
When I add a Comment, the form shows a drop down with all the possible Posts to link it to. This is not what I want. I want to hit a button inside a Post to create a comment. I don’t want the user to have to select the Post that they were just looking at.
Check out the site at ohsoccer.net.
Posts model:
-
-
<?php
-
-
class Post extends AppModel {
-
var $name = ‘Post’;
-
‘rule’ => ‘notEmpty’
-
),
-
‘rule’ => ‘notEmpty’
-
)
-
);
-
‘Comment’=>array(
-
‘className’=>‘Comment’,
-
‘foreignKey’=>‘post_id’,
-
‘dependent’=>true
-
)
-
);
-
}
-
?>
-
Comments model:
-
-
<?php
-
-
class Comment extends AppModel {
-
var $name = ‘Comment’;
-
‘rule’ => ‘notEmpty’
-
),
-
‘rule’ => ‘notEmpty’
-
),
-
‘rule’ => ‘notEmpty’
-
)
-
);
-
-
‘Post’=>array(
-
‘className’=>‘Post’
-
)
-
);
-
}
-
-
?>
-
Posts Controller:
-
-
<?php
-
-
class PostsController extends AppController {
-
-
‘limit’=>20,
-
‘order’=>array(
-
‘Post.created’ => ‘desc’
-
)
-
);
-
-
var $name = ‘Posts’;
-
-
function index() {
-
//$this->set(’posts’, $this->Post->find(’all’));
-
$data = $this->paginate(‘Post’);
-
$this->set(‘data’, $data);
-
}
-
-
function display() {
-
$this->set(‘posts’, $this->paginate(‘Post’));
-
}
-
-
function view($id = null) {
-
$this->Post->id = $id;
-
$this->set(‘post’, $this->Post->read());
-
}
-
-
function add() {
-
if ($this->Post->save($this->data)) {
-
$this->Session->setFlash(‘Your post has been saved.’);
-
}
-
}
-
}
-
-
function delete($id) {
-
$this->Post->del($id);
-
$this->Session->setFlash(‘The post with id: ‘.$id.‘ has been deleted.’);
-
}
-
-
function edit($id = null) {
-
$this->Post->id = $id;
-
$this->data = $this->Post->read();
-
} else {
-
if ($this->Post->save($this->data)) {
-
$this->Session->setFlash(‘Your post has been updated.’);
-
}
-
}
-
}
-
-
}
-
?>
-
Comments Controller:
-
-
<?php
-
class CommentsController extends AppController {
-
-
var $name = ‘Comments’;
-
-
function index() {
-
$this->set(‘comments’, $this->Comment->find(‘all’));
-
}
-
-
function view($id = null) {
-
$this->Comment->id = $id;
-
$this->set(‘comment’, $this->Comment->read());
-
}
-
-
function add() {
-
if ($this->Comment->save($this->data)) {
-
$this->Session->setFlash(‘Your comment has been saved.’);
-
}
-
}
-
$posts = $this->Comment->Post->find(‘list’);
-
}
-
-
function delete($id) {
-
$this->Comment->del($id);
-
$this->Session->setFlash(‘The comment with id: ‘.$id.‘ has been deleted.’);
-
}
-
-
function edit($id = null) {
-
$this->Comment->id = $id;
-
$this->data = $this->Comment->read();
-
} else {
-
if ($this->Comment->save($this->data)) {
-
$this->Session->setFlash(‘Your comment has been updated.’);
-
}
-
}
-
}
-
-
}
-
?>
-
Posts View Form:
-
-
-
<!– SHOW COMMENTS –>
-
<?php foreach($post[‘Comment’] as $comment):?>
-
<?php endforeach;?>
-
Comments Add Form:
-
-
<h1>Comment</h1>
-
<?php
-
//echo $form->input(’post_id’);
-
//echo $form->input(’post_id’, array(’type’=>’hidden’) );
-
//echo $form->input(’post_id’, array(’type’=>’hidden’, ‘value’ => $session->read(’Post.id’)));
-
//echo $form->input(’post_id’, array(’type’=>’hidden’));
-
//echo $form->hidden(’post_id’, array(’value’=>’13′));
-
-
//print_r($session->read());
-
?>
-



