【发布时间】:2017-07-11 05:13:55
【问题描述】:
我无法在 cakephp3 v3.2 中保存多条记录。我有一个字段可以在大约 20 行数据上重置为 0(见下文)。下面的代码不保存。我没有收到错误并且数据的调试是正确的,但是没有任何反应,没有错误。只是它不会将该字段重置为 0。
我可以使用下面的执行方法让它工作,但为什么新实体不起作用?
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'
}
【问题讨论】:
-
您为什么不尝试“saveMany()”来一次保存所有数据? @jagguy
-
我的版本是 3.2,所以它不会工作。
-
版本 >= 3.2.8 支持,所以更新你的作曲家。 @jagguy
-
之前没有更新,所以还不确定该怎么做,但无论如何为什么代码不适用于新实体?
-
在保存电话后,
$item->errors()有什么给你的吗?$result长什么样子?
标签: cakephp cakephp-3.0