【发布时间】:2011-04-04 06:16:05
【问题描述】:
我正忙于 cakePHP 的一个项目,我需要解析几个 XML 文件并将相关数据插入到我的 mysql 数据库中。脚本插入了它应该插入的内容,这不是问题。例如,如果我解析一两个文件(大约 7000-8000 条记录),就不会出错。
当我解析第三个或第四个 xml 文件时,问题就开始了。插入记录一分钟后,我看到数据库中成功插入了 9000-10000 条记录,但突然间脚本似乎重新启动。我注意到表中存在 0 条记录,它重新开始插入所有记录。所以脚本只需要很长时间才能执行。
短sn-p:
$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);
}
有人有这方面的经验吗?可能是什么原因以及更多可能的解决方案:)
谢谢
【问题讨论】:
-
你是如何传递文件的?那么在添加文件 1 和 2 时,您是否在添加文件 3 和 4 时重新添加它们?还是一次性添加文件 1、2、3 和 4?
标签: php mysql cakephp bulkinsert