【问题标题】:cant save mulitple records in cakephp3无法在 cakephp 3 中保存多条记录
【发布时间】: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


【解决方案1】:

什么是字段类型?如果字段类型和您插入的值不匹配,它将被封送。我的猜测是正在发生的事情。关键区别在于您正在投射的一个示例中,而在另一个示例中您没有投射,在某些示例中您使用 0 与 false。保持一致。

以下应该可以工作

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

你也可以逍遥法外

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2011-05-14
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多