2010-08-26 67 views
2

我忙于CakePHP中的一个项目,我需要解析几个XML文件并在mysql数据库中插入相关数据。脚本插入它应该插入的内容,这不是问题。例如,如果我解析一个或两个文件(大约7000-8000条记录),没有任何问题。脚本在一段时间后重新启动

问题从我解析第三个或第四个xml文件开始。插入记录一分钟后,我看到9000-10000个记录已成功插入到数据库中,但突然间脚本似乎重新启动。我注意到表中存在0条记录,它将重新插入所有记录。所以这个脚本只是花了很长时间才能执行。

一小段:

$content = simplexml_load_file($file); 

/** 
* Process line per line 
*/    
foreach ($content->product as $line) {    
    // create new record in products database table 
    $product = array(); 
    $product['Product']['productid'] = $line->attributes()->sku_number; 
    $product['Product']['name'] = $line->attributes()->name; 
    $product['Product']['description'] = empty($line->description->long) ? $line->description->short : $line->description->long; 
    $product['Product']['link'] = $line->URL->product; 
    $product['Product']['affiliate'] = 'linkshare'; 
    $product['Product']['price'] = $line->price->retail; 
    $product['Product']['brand'] = strtolower($line->brand); 
    $product['Product']['image'] = $line->URL->productImage; 

    // if not in rejectedproducts, save the new product to the database 
    if (!$rejectedproductModel->findByProductid($product['Product']['productid'])) { 
    $productModel->create(); 
    $productModel->save($product);   
} 

有人得到这方面的经验?可能是什么原因和更多什么可能是一个解决方案:)

谢谢

+0

你是如何传递这些文件的?所以当添加文件1和2时,你添加文件3和4时重新添加它们?或者你一次添加文件1,2,3和4? – 2010-08-26 13:05:13

回答

0

我会显示一些代码。 Feed的调用发生如下 parseDirectory方法检查指定文件夹中的所有xmls,并通过调用linkshare操作并传递文件名来解析它们。

function index() { 
     set_time_limit(0); 

     #$this->updateFeeds(); 

     App::import('Model', 'Product');   
     $productModel = new Product();  
     # truncate table products before adding new records to avoid duplicate records 
     $productModel->query('TRUNCATE TABLE products'); 

     # parse all files from shareasale 
     #$this->__parsedirectory('feeds/shareasale'); 
     # parse all files from linkshare 
     $this->__parsedirectory('feeds/linkshare'); 

     # send mails where necessary 
     $this->redirect(array('controller' => 'subscriptions', 'action' => 'sendmails')); 
    } 

私有函数

function __parsedirectory($dir) { 
    # retrieve name affiliate out of directory 
    $affiliate = explode('/', $dir); 
    $affiliate = $affiliate[1];  

    $dh = opendir($dir); 
    while (($file = readdir($dh)) !== false) { 
     if ($file != '.' && $file != '..' && !$this->__endswith($file, 'gz')) { 
      $this->requestAction('/parse/' . $affiliate . '/file:' . $file); 
      $this->Session->setFlash($affiliate . '/' . $file . ' parsed'); 
     } 
    } 
    closedir($dh); 
    $this->autoRender = false; 
} 
+0

您应该编辑您的问题,而不是将下一次的答案 – 2010-08-26 13:29:39

+0

哦,我看到 不知道 我会记住它 – Laurent 2010-08-26 13:32:13

0

我认为问题就出在这部分代码:

# truncate table products before adding new records to avoid duplicate records 
     $productModel->query('TRUNCATE TABLE products'); 

这是为了避免重复记录的好办法。这应该在DB上进行限制管理。这就是说,不知何故,这一小段代码在进程中间再次运行。

这是设置为CRON还是以某种方式自动运行?如果是这样,那么发生的是前一个文件在下一个文件开始时还没有完成解析。

+0

我不会做的TRUNCATE只是避免重复记录;)在产品表我只想成为当前在XML Feed中出现的产品。所以我需要将它完全清空,以便它们不会有任何旧记录;) 这段代码应该被执行。实际上,它似乎要运行好几次。问题是为什么:) 现在我自动运行它,但目标是在CRON作业投入生产时执行此操作。 我注意到一切顺利,当我在我的本地主机上执行相同的脚本。也许有内存问题的东西? – Laurent 2010-08-28 08:57:26

+0

我刚刚注意到这是'function index()',你在浏览器中运行这个吗?如果是这样,你应该考虑运行它的命令行。我敢打赌,命令行不会有同样的问题。这可能是在一段时间后,浏览器尝试重新加载页面。 – 2010-08-28 16:25:41

+0

我在浏览器中运行这个是的:)我会给它一个镜头,虽然我猜这个问题会持续下去,因为它在本地主机上运行时会很好。 thx – Laurent 2010-08-29 15:12:45

相关问题