magento内存问题,无法取消设置对象
我正在编写一个 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 和 unset($catModel) 行没有任何效果。我做错了什么?我怎样才能强制取消设置对象?!
i am writing a magento product exporter, that writes a couple of attributes into a csv file. one attribute is called the "category string" and its method looks like:
...
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;
}
but after each iteration the method call costs about 1MB for each product and i have about 9000 products! i cannot clean up the $catModel variable! the $catModel = NULL and unset($catModel) lines have no effects. what am i doing wrong? how can i force to unset the object?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们在 Magento 的 cron 上遇到了同样的问题,我知道这不是最好的方法,但我们需要快速完成。
我们的解决方案是创建一个新的 PHP 文件,其中包含执行一项操作所需的代码。从 magento 中,我们获取产品列表,然后使用 exec() 逐个产品地调用此外部 PHP 文件。
像这样:
希望有帮助。
We had the same problem with a cron for Magento, I know that it isn't the best way to do it but we needed to do it quickly.
Our solution was creating a new PHP file with the necessary code to do one single operation. From magento we get a product list and then call with exec() to this external PHP file product by product.
Something like this:
Hope it helps.
,然后选择必要类别,那么您的脚本会酷很多
更酷的事情是将类别名称直接加入到产品集合中,这将消耗更少的资源
so your script would be so much cooler if you
much much cooler thing would be joining the category names directly to product collection, that would consume even less resources