我有一段hasMany
关系(比如说Post
hasMany Comments
)
我想同时编辑Post
和现有Comment
的
我的代码
在我的评论edit.ctp
文件中我做了
<?= $this->Form->create($post); ?>
<?= $this->Form->input('title'); ?>
<?= $this->Form->input('title'); ?>
<?= $this->Form->input('text'); ?>
<?= $this->Form->input('comments.0.id'); ?>
<?= $this->Form->input('comments.0.text'); ?>
在我的PostsController
$post= $this->Posts->get($id);
$post= $this->Posts->patchEntity($post, $this->request->data, ['associated' => ['Comments']]);
我的问题
现在我希望评论会更新,而不是蛋糕每次都会添加新评论。
我做了什么
我试图调试$this->request->data
,我得到了
[
'text' => 'This is a test',
'comments' => [
(int) 0 => [
'id' => '168',
'text' => 'Comment test',
]
]
]
但如果我调试$post
我得到
object(App\Model\Entity\Post) {
/* ... */
'comments' => [
(int) 0 => object(App\Model\Entity\Comment) {
'text' => 'Comment test',
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'text' => true
],
/* ... */
}
],
/* ... */
'[dirty]' => [
'comments' => true,
],
}
id
那么,当我将评论传递给控制器时,为什么评论会被标记为“新”呢?
当然,这是我实际情况的过度简化版本。也许问题不在上面的代码中,我必须在其他代码中查看其他地方。
我的问题是我是否在做一些基本的方法错误。