2014-06-19 135 views
0

我正在尝试将超过10000行的文本文件导入数据库。我找到了一本手册here。但是我的脚本在没有任何错误的情况下到达最终的flush之前就结束了。我自定义刷新方法中的布尔参数代表刷新后调用clear方法。Doctrine2批量处理

代码:

$handle = fopen($file, 'r'); 
    if ($handle != FALSE) { 

     // Clear entities 
     $clear = 1; 
     // Read line 
     while (($data = fgets($handle)) !== FALSE) { 

      $entity = $this->_createEntity($data); 
      echo $clear . '<br>'; 
      $this->getPresenter()->getService('mapService')->persist($entity);     
      if ($clear % 100 == 0) { 
       echo 'saving...<br>'; 
       $this->getPresenter()->getService('mapService')->flush(TRUE); // Flush and clear 
      } 
      $clear++; 
     } 
     echo 'end...'; // Script ends before reaching this line 
     $this->getPresenter()->getService('mapService')->flush(); // Final flush 
     echo '...ed'; 
    } 
    fclose($handle); 

定制Flush方法:

public function flush($clear = FALSE) { 
    $this->db->flush(); 
    if ($clear) { 
     $this->db->clear(); 
    } 
} 

回声输出:

1 
... 
9998 
9999 
10000 
saving... 

但是,没有end......ed

非常感谢。

编辑

我已经改变了线路的数量中的文件以一个批量处理从10K到5000这是现在确定。但我仍然想知道为什么10k对于PHP或Doctrine来说太“太”了。

+0

很可能一些错误发生,将它使用一个“尝试..赶上”围绕while循环是有用的? –

+0

@RyanVincent,谢谢你的回复。我在发布之前做过。没有错误被抛出并被捕获。 –

+0

也许你必须分离你刷新的实体。 '的EntityManager ::明确()'。学说实体吃了很多RAM。 – DanFromGermany

回答

0

尝试使用FEOF:

$handle = fopen($file, 'r'); 
if ($handle != FALSE) { 

    // Clear entities 
    $clear = 1; 
    // Read line 
    //while (($data = fgets($handle)) !== FALSE) { 
    while (!feof($handle)){ 
     $data = fgets($handle); 

     $entity = $this->_createEntity($data); 
     echo $clear . '<br>'; 
     $this->getPresenter()->getService('mapService')->persist($entity);     
     if ($clear % 100 == 0) { 
      echo 'saving...<br>'; 
      $this->getPresenter()->getService('mapService')->flush(TRUE); // Flush and clear 
     } 
     $clear++; 
    } 
    echo 'end...'; // Script ends before reaching this line 
    $this->getPresenter()->getService('mapService')->flush(); // Final flush 
    echo '...ed'; 
} 
fclose($handle); 
+0

感谢您的回复。我尝试过,但没有任何改变。它可能无法识别文件的结尾,但记录看起来不错。 –