我正在编写一个magento产品导出器,它将一些属性写入csv文件。一个属性称为“类别字符串”,其方法如下:
...
foreach($products as $_product) {
...
$productId = $_product->getSku();
$productCategory = getCategoryString($_product['category_ids']);
...
}
...
function getCategoryString($numbers) {
$catString = '';
$catModel = Mage::getModel('catalog/category')->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID);
$ex = explode(',', $numbers);
foreach ($ex as $i => $e) {
if ($i > 0) {
$catString .= $catModel->load($e)->getName();
if ($i < (count($ex)-1))
$catString .= ' > ';
}
}
$ex = NULL;
$numbers = NULL;
$catModel->unsetData();
unset($catModel);
$catModel = NULL;
return $catString;
}
但每次迭代后,方法调用每个产品的成本约为1MB,我有大约9000个产品!我无法清理$ catModel变量! $ catModel = NULL和未设置($ catModel)行没有效果。我究竟做错了什么?我怎么强迫取消对象?!
答案 0 :(得分:1)
我们对Magento的cron有同样的问题,我知道这不是最好的方法,但我们需要快速完成。
我们的解决方案是创建一个新的PHP文件,其中包含执行单个操作所需的代码。从magento我们得到一个产品列表,然后用exec()调用这个外部PHP文件产品。
这样的事情:
foreach($products as $_product) {
...
exec("do_the_work.php {$_product->getSku()}");
...
}
希望它有所帮助。
答案 1 :(得分:0)
所以如果你
你的剧本会更酷更酷的是将类别名称直接加入到产品集合中,这将消耗更少的资源