【发布时间】:2016-05-21 16:18:13
【问题描述】:
我对 CakePHP 还很陌生,虽然我觉得我有一些基本的了解。
我已经基于“文章”表和bake all 制作了一个基本博客,到目前为止,小菜一碟;D。现在我添加了一个“cmets”表。 'articles' 有许多 'cmets' 和 'cmets' 属于 'articles'。我再次为这两个表baked all 并编辑了ArticlesController.php 和Articles/view.ctp 中的'view' 操作以显示一篇文章的所有cmets。还没有问题。
现在我希望能够在文章的“查看”页面上添加评论,就像您可以在此论坛上发表评论一样。所以我在 view.ctp 中添加了一个Html->Form 并将评论的 add() 中的一些部分复制到文章的 view() 中。文章的查看操作:
public function view($id = null) {
$article = $this->Articles->get($id, [
'contain' => ['Comments']
]);
// Part from the add-action from Comments
$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.'));
}
}
// Set the selected article
$this->set('article', $article);
$this->set('_serialize', ['article']);
}
Articles/view.ctp 的一部分:
<?php foreach ($article->comments as $comment) : ?>
<h5><?= $comment->author ?></h5>
<p><?= $comment->body ?></p>
<?php endforeach; ?>
<b>Add comment</b>
<?= $this->Form->create($comment) ?>
<?php
echo $this->Form->input('comment.author');
echo $this->Form->input('comment.body');
?>
<?= $this->Form->button(__('Submit Comment')) ?>
<?= $this->Form->end() ?>
但这给了我一个致命的错误,:
错误:在布尔文件上调用成员函数 newEntity() C:\xampp\htdocs\blog_simple\src\Controller\ArticlesController.php 线路:45
关于如何完成我正在寻找的任何建议?
【问题讨论】:
标签: cakephp view cakephp-3.0 edit