如何使用直接连接到数据库的方式在 Magento 中导入产品

发布于 2024-09-28 06:29:07 字数 1649 浏览 3 评论 0原文

我有大约 50.000 条记录要导入到 Magento 商店中。我已经测试过的: 该文件大约 50 MB。

  • 拆分文件
  • API
  • Magento 类

拆分文件不会提高产品导入的速度。 Api 非常慢。 Magento 类很慢。

这是使用 Magento 类的代码片段:

// Build the product
$product->setIsMassupdate(true)
        ->setExcludeUrlRewrite(true)
        ->setManufacturer($this->addManufacturers(utf8_encode($record[4])))
        ->setSku($record[3])
        ->setAttributeSetId($this->attribute_set)# 9 is for default
        ->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
        ->setName(utf8_encode($record[5]))
        ->setCategoryIds($this->getCategories(array($record[0], $record[1], $record[2]))) # some cat id's,
        ->setWebsiteIDs(array(1)) # Website id, 1 is default
        ->setDescription(utf8_encode($record[6]))
        ->setShortDescription($this->shortText(utf8_encode($record[6]), 150))
        ->setPrice($price) # Set some price
        ->setSpecialPrice($special_price)
        ->setWeight($record[12])
        ->setStatus( Mage_Catalog_Model_Product_Status::STATUS_ENABLED )
        ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
        ->setTaxClassId(2)     // default tax class
        ->setPixmaniaimg($record[10])
        ->setStockData(array('is_in_stock' => $inStock, 'qty' => $qty))
        ->setCreatedAt(strtotime('now'));

$product->save();     
$ID = is_numeric($productID) ? $productID : $product->getId(); 

因此上述方法是正确的,但它花费了大约 5 个小时才能仅插入 2300 条记录!

这是我必须执行的简单 SQL 插入在 Magento DB 中添加新产品?

I have about 50.000 of records to import in a Magento store. What I have already tested:
The file is about 50 MB.

  • Splitted files
  • API
  • Magento Classes

Splitting the file doesn't improve the speed of the importing of the products.
Api are very slow.
Magento Classes are slow.

This is a snipped of code using the Magento Classes:

// Build the product
$product->setIsMassupdate(true)
        ->setExcludeUrlRewrite(true)
        ->setManufacturer($this->addManufacturers(utf8_encode($record[4])))
        ->setSku($record[3])
        ->setAttributeSetId($this->attribute_set)# 9 is for default
        ->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
        ->setName(utf8_encode($record[5]))
        ->setCategoryIds($this->getCategories(array($record[0], $record[1], $record[2]))) # some cat id's,
        ->setWebsiteIDs(array(1)) # Website id, 1 is default
        ->setDescription(utf8_encode($record[6]))
        ->setShortDescription($this->shortText(utf8_encode($record[6]), 150))
        ->setPrice($price) # Set some price
        ->setSpecialPrice($special_price)
        ->setWeight($record[12])
        ->setStatus( Mage_Catalog_Model_Product_Status::STATUS_ENABLED )
        ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
        ->setTaxClassId(2)     // default tax class
        ->setPixmaniaimg($record[10])
        ->setStockData(array('is_in_stock' => $inStock, 'qty' => $qty))
        ->setCreatedAt(strtotime('now'));

$product->save();     
$ID = is_numeric($productID) ? $productID : $product->getId(); 

So the above method is correct but it spends about 5 hours in order to insert only 2300 records!!

Which are the simple SQL inserts that I have to execute in the Magento DB in order to add a new product?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尸血腥色 2024-10-05 06:29:07

我强烈建议您不惜一切代价避免编写原始 SQL,您几乎肯定会花费数天时间来编写映射属性 ID,并且可能会出错。它还将绕过 Magento 所依赖的所有重要索引和其他系统更新。

如果速度是您的问题,我建议您考虑 Unirgy 的 uRapidFlow。通常的免责声明适用,我与 Unirgy 没有任何关系,但我的观察是这项工作的质量非常出色。

哈特哈,
京东

I strongly recommend that you avoid writing raw SQL at all costs, you will almost certainly spend days and days writing to map the attribute IDs and probably get it wrong. It will also bypass all the important indexing and other system updates that Magento relies on.

If speed is your issue, I suggest that you consider uRapidFlow from Unirgy. Usual disclaimers apply, I have no affiliation with Unirgy, but my observations has been that the quality of this work is excellent.

HTH,
JD

满身野味 2024-10-05 06:29:07

如果您在加载运行时禁用索引器,然后重新启用并运行,则应该可以缩短加载时间。

$indexer = Mage::getSingleton('index/indexer');
$indexer->lockIndexer();

// ... run your processing ...

$indexer->unlockIndexer();

// Reindex everything
$processes = $indexer->getProcessesCollection();
foreach ($processes as $process)
{
    // echo 'Processing: ' . $process->getIndexerCode() . "n";
    $process->reindexEverything();
}

If you disable the indexer while your load runs and then re-enable and run afterwards, it should improve your load time.

$indexer = Mage::getSingleton('index/indexer');
$indexer->lockIndexer();

// ... run your processing ...

$indexer->unlockIndexer();

// Reindex everything
$processes = $indexer->getProcessesCollection();
foreach ($processes as $process)
{
    // echo 'Processing: ' . $process->getIndexerCode() . "n";
    $process->reindexEverything();
}
顾挽 2024-10-05 06:29:07

使用原始 SQL 查询创建产品非常困难,因为 Magento 使用 EAV 模式来存储产品。

It's very hard to create products using raw SQL queries, because Magento uses EAV pattern for storing products.

面犯桃花 2024-10-05 06:29:07

有时我注意到批量插入的工作方式是首先创建一个模板模型...

$blankProduct = Mage::getModel('catalog/product');

...然后避免为每个记录创建模型...

$newProduct = clone $blankProduct;
$newProduct->setIsMassupdate(true)
    ...
$newProduct->save();

这稍微更有效,但可能不足以将大量导入合理地导入时间。

Occasionally I've noticed bulk inserts that work by first creating a template model...

$blankProduct = Mage::getModel('catalog/product');

...then avoid the creation of the model for each record...

$newProduct = clone $blankProduct;
$newProduct->setIsMassupdate(true)
    ...
$newProduct->save();

It's slightly more efficient but probably not enough to get that massive import to a reasonable time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文