jagguy
July 11, 2016, 6:33am
1
Hi, I cant save (update) associated form data for model AvailabilityForTutors that has a hasMany relationship with the Primary model. I used checkboxes to update some options for the ‘AvailabilityForTutors. not_available’ field. I added in id’s but that made no difference. The updated ‘AvailabilityForTutors. not_available’ field is not being updated in the database. I dont get an error and the primary model data for the Tutor does save.
I checked the docs and past posts for this straightforward task and I cant get it. I am doing something wrong.
//controller
$tutor = $this->Tutors->get($id, [
‘contain’ => [‘AvailabilityForTutors’]
]);
if ($this->request->is([‘patch’, ‘post’, ‘put’])) {
debug($this->request->data);
$tutor = $this->Tutors->patchEntity($tutor, $this->request->data,['associated' => ['AvailabilityForTutors'],'validate' => false ] );
if ($this->Tutors->save($tutor)) {
$this->Flash->success(__('The tutor has been saved.'));
return $this->redirect(['action' => 'edittest2',$id]);
} else {
$this->Flash->error(__('The tutor could not be saved. Please, try again.'));
}
}
//view
<?php
echo $this->Form->hidden('id', ['value'=>$id]);
echo $this->Form->input('first_name',['label' => 'Tutor FirstName']);
echo $this->Form->input('last_name',['label' => 'Tutor LastName']);
foreach ($tutor->availability_for_tutors as $key => $item) {
echo $this->Form->hidden('AvailabilityForTutors.'.$key.'.id', ['value'=>$item->id]);
echo $this->Form->input('AvailabilityForTutors.'.$key.'.not_available', ['label'=>$item->weekday,
'checked'=> $item->not_available,'type'=>'checkbox']);
}
model tutor//
$this->hasMany('AvailabilityForTutors', [
'foreignKey' => 'tutor_id'
]);
posted data which is correct
'id' => '12',
'first_name' => 'fred',
'last_name' => 'Tow',
'AvailabilityForTutors' => [
(int) 0 => [
'id' => '36',
'not_available' => '1'
],
(int) 1 => [
'id' => '37',
'not_available' => '0'
],
(int) 2 => [
'id' => '38',
'not_available' => '0'
1 Like
I have same issue @ 3.3
I can add new records, but can’t edit / update.
What is wrong with this code?
//JobsController
public function edit($id = null)
{
$job = $this->Jobs->get($id, [
'contain' => ['Locations', 'Questions']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
//debug($this->request->data); <-------- line 103
$job = $this->Jobs->patchEntity(
$job,
$this->request->data,
[
'associated' => [
'Locations' => [
'accessibleFields' => ['id' => true]
],
'Questions' => [
'accessibleFields' => ['id' => true]
]
]
]
);
if ($this->Jobs->save($job)) {
$this->Flash->success(__('The job has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The job could not be saved. Please, try again.'));
}
}
//...
}
//JobsTable
$this->hasMany('Questions', [
'foreignKey' => 'job_id',
'className' => 'Settings.Questions',
'dependent' => true
]);
//edit.ctp
<?php
foreach($job->questions as $key => $value) { ?>
<?= $this->Form->hidden(sprintf('questions.%s.id', $key)); ?>
<?= $this->Form->input(sprintf('questions.%s.question', $key)); ?>
<?php } ?>
debug update
\plugins\Settings\src\Controller\JobsController.php (line 103)
[
'title' => 'bla bla',
'company_id' => 'c6f107c1-66d5-4bb4-bbf2-614a0aaf7e6c',
'locations' => [
'_ids' => [
(int) 0 => '1088463'
]
],
'questions' => [
(int) 0 => [
'id' => '17',
'question' => 'ffffff uuu' // updated field
],
(int) 1 => [
'id' => '18',
'question' => 'fffggggg'
]
],
'id' => '18'
]
Update is not saved. But I can add a new question ???
Please help.
If i create form fields in this way, all works.
<?= $this->Form->hidden('questions.0.id'); ?>
<?= $this->Form->input('questions.0.question'); ?>
<?= $this->Form->hidden('questions.1.id'); ?>
<?= $this->Form->input('questions.1.question'); ?>
What is wrong?
EDIT:
I test it in various ways:
If we have four records (questions) in the database and create for them, form fields, update does not work, but if we are reduced to 3 form field, update works for these field.
This is very confusing.
is CakePHP bug https://github.com/cakephp/cakephp/issues/9267