【发布时间】:2016-09-03 20:18:45
【问题描述】:
我有一个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']]);
我的问题
现在我希望评论会更新,而不是 cake 每次都会添加新评论。
我做了什么
我尝试调试$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 传递给控制器时,为什么评论会被标记为“新”?
当然,这是我实际情况的过度简化版本。也许问题不在上面的代码中,我必须在我的其他代码中寻找其他地方。
我的问题是我是否犯了一些基本的方法错误。
【问题讨论】:
-
您可以删除旧记录以保存新记录。
标签: php cakephp cakephp-3.2