从另一个模型 (CakePHP 3) 的视图编辑关联模型
Edit associated model from view of another model (CakePHP 3)
我对 CakePHP 还是很陌生,尽管我认为我有一些基本的了解。
我已经创建了一个基于 'articles' table 和 bake all
的基本博客,到目前为止小菜一碟;D。现在我添加了一个 'comments' table。 'articles' 有许多 'comments' 和 'comments' 属于 'articles'。我再次对 table 进行了 baked all
,并在 ArticlesController.php 和 Articles/view.ctp 中编辑了 'view' 操作以显示一篇文章的所有评论。还没有问题。
现在我希望能够在文章的 'view' 页面上添加评论,就像您可以在此论坛上发表评论一样。所以我在 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() ?>
但这给了我一个致命错误,:
Error: Call to a member function newEntity() on boolean File
C:\xampp\htdocs\blog_simple\src\Controller\ArticlesController.php
Line: 45
关于如何完成我正在寻找的东西有什么建议吗?
Error: Call to a member function newEntity() on boolean File
C:\xampp\htdocs\blog_simple\src\Controller\ArticlesController.php
Line: 45
因为您在 Articles
Controller 中并且您正在尝试 Comments
相关功能(没有加载模型)。
你有两个选择。
如果您设置了正确的关系,则将 Articles
附加到调用中,例如
$comment = $this->Comments->newEntity();
至
$comment = $this->Articles->Comments->newEntity();
对所有评论PatchEntity
和Save
功能做同样的事情。
添加
$this->loadModel('Comments');
在调用 Comments
相关函数之前。无需像上一点中提到的那样附加 Articles
。因为,我们正在加载模型。
试试你喜欢哪一个。祝你好运!
我对 CakePHP 还是很陌生,尽管我认为我有一些基本的了解。
我已经创建了一个基于 'articles' table 和 bake all
的基本博客,到目前为止小菜一碟;D。现在我添加了一个 'comments' table。 'articles' 有许多 'comments' 和 'comments' 属于 'articles'。我再次对 table 进行了 baked all
,并在 ArticlesController.php 和 Articles/view.ctp 中编辑了 'view' 操作以显示一篇文章的所有评论。还没有问题。
现在我希望能够在文章的 'view' 页面上添加评论,就像您可以在此论坛上发表评论一样。所以我在 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() ?>
但这给了我一个致命错误,:
Error: Call to a member function newEntity() on boolean File C:\xampp\htdocs\blog_simple\src\Controller\ArticlesController.php Line: 45
关于如何完成我正在寻找的东西有什么建议吗?
Error: Call to a member function newEntity() on boolean File C:\xampp\htdocs\blog_simple\src\Controller\ArticlesController.php Line: 45
因为您在 Articles
Controller 中并且您正在尝试 Comments
相关功能(没有加载模型)。
你有两个选择。
如果您设置了正确的关系,则将
Articles
附加到调用中,例如$comment = $this->Comments->newEntity();
至
$comment = $this->Articles->Comments->newEntity();
对所有评论PatchEntity
和Save
功能做同样的事情。
添加
$this->loadModel('Comments');
在调用
Comments
相关函数之前。无需像上一点中提到的那样附加Articles
。因为,我们正在加载模型。
试试你喜欢哪一个。祝你好运!