Reputation: 183
I cant save multiple records in cakephp3 v3.2. I have a field to reset to 0 on about 20 rows of data (see below). The below code doesnt save. I dont get an error and a debug of the data is correct but nothing happens, no errors. Just it doesnt reset that field to 0.
I can get it to work using execute method as below but why doesnt newentites work?
private function tmpschedules( $tmpscheduleStudents=0, $tutorId=0){
foreach ( $tmpscheduleStudents as $key => $item) :
// debug($item);
$tmpscheduleStudents[$key]['allocated']=0;
endforeach;
// exit;
$students_schedule = $this->Tmpschedules->newEntities($tmpscheduleStudents, ['validate' => false]);
foreach ( $students_schedule as $key => $item) {
debug($item);
$result=$this->Tmpschedules->save($item, ['atomic' => false]);
}
exit;
return 1;
}
//see allocated field is 0 before being saved but it doesnt save.
object(App\Model\Entity\Tmpschedule) {
'student_id' => (int) 1039,
'tutor_id' => (int) 92,
'rank' => (int) 0,
'inactive' => (int) 0,
'weekday_start' => (int) 0,
'start_order' => (int) 0,
'allocated' => (int) 0,
//this does work
foreach ( $tmpscheduleStudents as $key => $item) :
// debug($item);
// $tmpscheduleStudents[$key]['allocated']=0;
$query = $this->Tmpschedules->query();
$query->update()
->set(['allocated' => 0])
->where(['tutor_id' => $tutorId])
->execute();
endforeach;
//result update which has allocated as o0 but doesnt save and no errors as you see below
'student_id' => (int) 245,
'tutor_id' => (int) 13,
'rank' => (int) 0,
'inactive' => (int) 0,
'weekday_start' => (int) 0,
'start_order' => (int) 0,
'allocated' => (int) 0,
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'student_id' => true,
'tutor_id' => true,
'rank' => true,
'inactive' => true,
'weekday_start' => true,
'start_order' => true,
'allocated' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Tmpschedule2s'
}
Upvotes: 1
Views: 78
Reputation: 446
What is the field type? If the field type and the value you are inserting do not match it will be getting marshaled away. My guess is that is what is happening. The key difference is in one of your examples you are casting and in the other you are not casting also in some you use 0 vs false. Keep consistent.
The following should work
$students_schedule = $this->Tmpschedules->newEntities($tmpscheduleStudents, ['validate' => 0]);
You could also get away with
$students_schedule = $this->Tmpschedules->newEntities($tmpscheduleStudents, ['validate' => (int)false]);
just make sure that your column type and value type match and that should resolve your issue. My example assumes you are using a numeric type field.
Upvotes: 0