I have a hasMany
relationship (let's say Post
hasMany Comments
)
I want to edit both the Post
and an existing Comment
at the same time
My code
in my comment edit.ctp
file I did
<?= $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'); ?>
and in my PostsController
$post= $this->Posts->get($id);
$post= $this->Posts->patchEntity($post, $this->request->data, ['associated' => ['Comments']]);
My problem
now I expect that the comment will be updated, instead cake adds a new comment every time.
What did I do
I tried to debug $this->request->data
and I got
[
'text' => 'This is a test',
'comments' => [
(int) 0 => [
'id' => '168',
'text' => 'Comment test',
]
]
]
but if I debug $post
I get
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,
],
}
So why comment is marked as 'new' when I pass its id
to the controller?
Of course this is an over simplified version of my actual situation. Maybe the problem it's not in the above code and i have to look elsewhere in my other code.
My question here is if I'm doing some basic approach error.