只能保存数组cakephp3

时间:2016-10-02 02:35:54

标签: php cakephp

由于某种原因我无法保存所有多条记录,因为我刚刚保存了最后一条记录。我尝试过像atomic和validate这样的选项,但它没有解决问题。我没有得到一个错误信息,为什么它不是全部保存,所以我浪费时间只是猜测。

发生的事情是我保存了大约7条记录的最后一条记录,因此它只保存了一次。我只保存在1个没有关联的表中。没有错误输出我不知道该怎么做

 $alltimesheetLessons = $this->TimesheetLessons->newEntities( $timesheetLesson, ['validate' =>  false]);


                foreach ( $alltimesheetLessons as $key => $entity) {
               // debug($entity->toArray());
                  $this->TimesheetLessons->save($entity, ['atomic' =>  false]);

                }//for 

//这里是2条记录的样本

'tutor_id' => (int) 12,
'subject_id' => (int) 16,
'lesson_plan' => 'q1)Evidence:',
'lesson_date' => object(Cake\I18n\FrozenDate) {

    'time' => '2016-09-20T00:00:00+00:00',
    'timezone' => 'UTC',
    'fixedNowTime' => false

},
'period_start_date' => object(Cake\I18n\FrozenDate) {

    'time' => '2016-09-19T00:00:00+00:00',
    'timezone' => 'UTC',
    'fixedNowTime' => false

},
'period_end_date' => object(Cake\I18n\FrozenDate) {

    'time' => '2016-10-02T00:00:00+00:00',
    'timezone' => 'UTC',
    'fixedNowTime' => false

},
'lesson_id' => (int) 18913,
'submit_date' => '2016-10-02',
'invoice' => '49',
'term_id' => (int) 0,
'start_time' => '10:00 ',
'end_time' => '11:00 ',
'student_name' => 'jjde',
'class_year' => (int) 8,
'subjectname' => 'Maths: Year 7 - 10',
'makeup_lesson' => false,
'makeup_lesson_expired' => false,
'makeup_expiry_date' => null,
'lesson_inactive' => false,
'cancelled_by' => '',
'schedule_rec' => false,
'forefit' => (int) 0,
'cancelled_date' => null,
'tutoring_type_id' => (int) 1,
'payrate' => (float) 20,
'lesson_duration' => (float) 1,
'amount' => (float) 20

 \src\Controller\LessonsController.php (line 2590)

[
    'tutor_id' => (int) 12,
    'subject_id' => (int) 16,
    'lesson_plan' => 'q1)EvidimplestvformHomeWork:',
    'lesson_date' => object(Cake\I18n\FrozenDate) {

        'time' => '2016-09-20T00:00:00+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    'period_start_date' => object(Cake\I18n\FrozenDate) {

        'time' => '2016-09-19T00:00:00+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    'period_end_date' => object(Cake\I18n\FrozenDate) {

        'time' => '2016-10-02T00:00:00+00:00',
        'timezone' => 'UTC',
        'fixedNowTime' => false

    },
    'lesson_id' => (int) 18914,
    'submit_date' => '2016-10-02',
    'invoice' => '49',
    'term_id' => (int) 0,
    'start_time' => '11:00 ',
    'end_time' => '12:00 ',
    'student_name' => 'Jhj',
    'class_year' => (int) 8,
    'subjectname' => 'Maths: Year 7 - 10',
    'makeup_lesson' => false,
    'makeup_lesson_expired' => false,
    'makeup_expiry_date' => null,
    'lesson_inactive' => false,
    'cancelled_by' => '',
    'schedule_rec' => false,
    'forefit' => (int) 0,
    'cancelled_date' => null,
    'tutoring_type_id' => (int) 1,
    'payrate' => (float) 20,
    'lesson_duration' => (float) 1,
    'amount' => (float) 20
]

 foreach ($data as  $key => $item):



                $timesheetLesson[$key]['tutor_id']=$item['tutor_id'];
                $timesheetLesson[$key]['subject_id']=$item['subject_id'];
                $timesheetLesson[$key]['lesson_plan']=$item['lesson_plan'];
                $timesheetLesson[$key]['lesson_date']=date('Y-m-d', strtotime($item['lesson_date']));
                $timesheetLesson[$key]['period_start_date']=date('Y-m-d', strtotime($startdate));
                $timesheetLesson[$key]['period_end_date']=date('Y-m-d', strtotime($enddate));

                $timesheetLesson[$key]['lesson_id']=$item['id'];
                $timesheetLesson[$key]['submit_date'] = date('Y-m-d');
                $timesheetLesson[$key]['invoice']=$invoice;

                $timesheetLesson[$key]['term_id']=$item['term_id'];
                $timesheetLesson[$key]['start_time']=date('h:i ', strtotime($item['start_time']));
                $timesheetLesson[$key]['end_time']=date('h:i ', strtotime($item['end_time']));


              endforeach;

3 个答案:

答案 0 :(得分:0)

$result =将覆盖数组的每次迭代;让它填充最后一个循环。要构建新阵列,您可以使用:

$result[] =

将使用每个元素填充$ result数组。

答案 1 :(得分:0)

您需要将结果推送到数组$result。像这样:

$result[] = ...;
       ^^

或者这个:

array_push($result, $value_pushed);

答案 2 :(得分:0)

我发现错误,因为有一个构建规则正在为正在输入的字段停止0条目。即使验证为false,构建规则仍处于活动状态