CakePHP 3快速插入/更新记录

时间:2018-03-06 19:15:40

标签: php cakephp foreach

我正在尝试使用foreach循环插入/更新+/- 10k行。完整的循环持续时间约为3-5分钟。我的代码是否有任何提示更快地插入更新?从转换为domdocument的xls文件中检索$行。

foreach($rows as $key => $row)
{
    if($key < 1){continue;}
    $cells = $row -> getElementsByTagName('td');
    foreach ($cells as $cell) {

        $project_id = $cells[0]->nodeValue;
        $title = $cells[1]->nodeValue;
        $status = $cells[2]->nodeValue;
        $projectmanager = $cells[3]->nodeValue;
        $engineer = $cells[4]->nodeValue;
        $coordinator = $cells[5]->nodeValue;
        $contractor_a = $cells[6]->nodeValue;
        $contractor_b = $cells[7]->nodeValue;
        $gsu = $cells[9]->nodeValue;
        $geu = $cells[10]->nodeValue;

        $query = $this->Projects->find('all')->select(['project_id'])->where(['project_id' => $project_id]);
        if ($query->isEmpty()) {

            $project = $this->Projects->newEntity();
            $project->title = $title;
            $project->project_id = $project_id;
            $project->status = $status;
            $project->projectmanager = $projectmanager;
            $project->engineer = $engineer;
            $project->coordinator = $coordinator;
            $project->contractor_a = $contractor_b;
            $project->contractor_b = $contractor_a;
            $project->gsu = date("Y-m-d H:i:s");
            $project->geu = date("Y-m-d H:i:s");
            $project->gsm = date("Y-m-d H:i:s");
            $project->gem = date("Y-m-d H:i:s");

            if ($this->Projects->save($project)) {
                //$this->Flash->success(__('The project has been saved.'));
                continue;
            }else{
                debug($project->errors());
            }
        }else{
            continue;
            $query->title = $title;
            $query->status = $status;
            $query->projectmanager = $projectmanager;
            $query->engineer = $engineer;
            $query->coordinator = $coordinator;
            $query->contractor_a = $contractor_b;
            $query->contractor_b = $contractor_a;
            $query->gsu = $gsu;
            $query->geu = $geu;

            if ($this->Projects->save($query)) {
                //$this->Flash->success(__('The project has been saved.'));
                continue;
            }
        }
    }
    //$this->Flash->error(__('The project could not be saved. Please, try again.'));
}

2 个答案:

答案 0 :(得分:1)

对于更快的批量插入,不要使用实体,而是直接生成插入查询。 https://book.cakephp.org/3.0/en/orm/query-builder.html#inserting-data

答案 1 :(得分:0)

埃洛,我的朋友。 保存单个记录时,TableClass-&gt; save()方法很有用,在您的情况下,您应该使用TableClass-&gt; saveMany()。 为此,您需要将实体视为foreach中的数组。 在foreach之后,您将使用tableclass(newEntities)中的另一个方法将数组转换为实体,然后再保存它们。

基本示例:

//Lets supose our array after our foreach become something like this:
$all_records = 
  [
      //Each item will be an array, not entities yet
      [
          'name'    => 'I.N.R.I.',
          'year'    => '1987',
          'label'   => 'Cogumelo',
          'country' => 'Brazil',
          'band'    => 'Sarcófago',
          'line_up' => '[{"name":"Wagner Antichrist","role":"Vomits, Insults"},{"name":"Gerald Incubus","role":"Damned Bass"},{"name":"Z\u00e9der Butcher","role":"Rotten Guitars"},{"name":"D.D. Crazy","role":"Drums Trasher"}]'
      ],
      //Another record coming in..
      [
          'name'    => 'Eternal Devastation',
          'year'    => '1986',
          'label'   => 'Steamhammer',
          'country' => 'Germany',
          'band'    => 'Destruction',
          'line_up' => '[{"name":"Marcel Schmier","role":"Vocals, Bass"},{"name":"Mike Sifringer","role":"Guitars"},{"name":"Tommy Sandmann","role":"Drums"}]'
      ]
  ];

//Time to get the tableclass...
$albums   = TableRegistry::get('Albums');

//Time to transform our array into Album Entities
$entities = $albums->newEntities($all_records);

//Now, we have transformed our array into entities on $entities, this is the variable we need to save
if(!$albums->saveMany($entities))
  {
      echo "FellsBadMan";
  }
else
  {
      echo "FellsGoodMan";
  }

您可以阅读有关here

的更多信息