Php Doctrine2批处理
我正在尝试将包含10000多行的文本文件导入DB。我找到了一本手册。但是我的脚本在到达最后的Php Doctrine2批处理,php,doctrine-orm,Php,Doctrine Orm,我正在尝试将包含10000多行的文本文件导入DB。我找到了一本手册。但是我的脚本在到达最后的flush之前结束,没有任何错误。自定义刷新方法中的布尔参数表示刷新后调用clear方法 代码: 回波输出: 1 ... 9998 9999 10000 saving... 但是没有结束……ed 先谢谢你 编辑 我已将文件中的行数从10k更改为一批处理5000。现在没事了。但我仍然想知道为什么10k对于PHP或条令来说“太多了”。尝试使用feof: $handle = fopen($file, 'r')
flush
之前结束,没有任何错误。自定义刷新方法中的布尔参数表示刷新后调用clear
方法
代码:
回波输出:
1
...
9998
9999
10000
saving...
但是没有结束……ed
先谢谢你
编辑
我已将文件中的行数从10k更改为一批处理5000。现在没事了。但我仍然想知道为什么10k对于PHP或条令来说“太多了”。尝试使用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);
$handle=fopen($file,'r');
如果($handle!=FALSE){
//清除实体
$clear=1;
//读线
//while(($data=fgets($handle))!==FALSE){
而(!feof($handle)){
$data=fgets($handle);
$entity=$this->\u createEntity($data);
回显$clear。“
”;
$this->getPresenter()->getService('mapService')->persist($entity);
如果($clear%100==0){
回显“正在保存…”;
$this->getPresenter()->getService('mapService')->flush(TRUE);//flush并清除
}
$clear++;
}
echo'end…';//脚本在到达此行之前结束
$this->getPresenter()->getService('mapService')->flush();//最终刷新
回声“…ed”;
}
fclose($handle);
感谢您的回复。我尝试过,但没有任何更改。它可能无法识别文件的结尾,但记录看起来正常。很可能发生了一些错误,在while循环中使用“try..catch”是否有用?@RyanVincent,感谢您的回复。我在发布之前就这样做了。没有抛出错误并捕获。也许您必须取消tach您已刷新的实体。EntityManager::clear()
。条令实体会消耗大量RAM。@Danfrom Germany,在刷新100条记录后,调用“clear”方法。@PeterO。flush
不会分离实体,它只会清除UnitOfWork
。
1
...
9998
9999
10000
saving...
$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);