我是cakephp的新手,我刚刚完成了博客/文章教程,现在我正在尝试添加评论系统。
在我的注释表中,我有Id、article_Id、name、email、text和created。
我尝试根据文章ID添加评论,假设用户查看了一篇特定的文章,比如/view/1,他可以在页面底部为该文章添加评论。我该怎么做呢?我希望我说清楚了。
这是我对我的评论的添加函数:
public function add()
{
$comment = $this->Comments->newEntity();
if ($this->request->is('post')) {
$comment = $this->Comments->patchEntity($comment, $this->request-
>data);
if ($this->Comments->save($comment)) {
$this->Flash->success('The comment has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The comment could not be saved. Please, try
again.');
}
}
$articles = $this->Comments->Articles->find('list', ['limit' => 200]);
$this->set(compact('comment', 'articles'));
$this->set('_serialize', ['comment']);
}和我的add视图:
<div class="comments form large-10 medium-9 columns">
<?= $this->Form->create($comment); ?>
<fieldset>
<legend><?= __('Add Comment') ?></legend>
<?php
echo $this->Form->input('article_id', ['options' => $articles]);
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('text');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
正如你所看到的,我正在从我的下拉列表中选择我的文章,但我不想这样做,我希望当用户查看该特定文章时,他们可以直接评论和查看。
发布于 2015-05-06 20:29:58
在该视图页面上添加评论表单-
<div class="comments form large-10 medium-9 columns">
<?= $this->Form->create($comment); ?>
<fieldset>
<legend><?= __('Add Comment') ?></legend>
<?php
echo $this->Form->hidden('article_id', ['value' => $articleId]);
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('text');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>并为具有文章id的值的article_id设置隐藏字段。
https://stackoverflow.com/questions/30076705
复制相似问题