不能在cakephp3中保存多个记录

时间:2017-07-11 05:13:56

标签: cakephp cakephp-3.0

我无法在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'

    } 

1 个答案:

答案 0 :(得分:0)

什么是字段类型?如果字段类型和您要插入的值不匹配,它将被封送。我的猜测是正在发生的事情。关键的区别在于你正在施放的一个例子中,另一个你也没有施放你使用0和假的一些。保持一致。

以下内容应该有效

$students_schedule = $this->Tmpschedules->newEntities($tmpscheduleStudents, ['validate' =>  0]);

你也可以逃脱

$students_schedule = $this->Tmpschedules->newEntities($tmpscheduleStudents, ['validate' =>  (int)false]);

只需确保您的列类型和值类型匹配,这应该可以解决您的问题。我的示例假设您使用的是数字类型字段。