magento内存问题,无法取消设置对象

发布于 2024-11-12 23:49:39 字数 1012 浏览 3 评论 0原文

我正在编写一个 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 技术交流群。

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

发布评论

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

评论(2

倚栏听风 2024-11-19 23:49:39

我们在 Magento 的 cron 上遇到了同样的问题,我知道这不是最好的方法,但我们需要快速完成。

我们的解决方案是创建一个新的 PHP 文件,其中包含执行一项操作所需的代码。从 magento 中,我们获取产品列表,然后使用 exec() 逐个产品地调用此外部 PHP 文件。

像这样:

foreach($products as $_product) {       
        ...

        exec("do_the_work.php {$_product->getSku()}");

        ...
}

希望有帮助。

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:

foreach($products as $_product) {       
        ...

        exec("do_the_work.php {$_product->getSku()}");

        ...
}

Hope it helps.

傾旎 2024-11-19 23:49:39

,然后选择必要类别,那么您的脚本会酷很多

  1. 因此,如果您从所有产品中获取所有目录 ID 作为数组,
  2. 所需的类别名称
  3. 使用 IN() 子句加载类别集合,并再次使用每个产品集合 id 来自预加载的集合,其中包含正确的元素,
  4. 利润 $$$$ 因为您没有使用内存,就像它对您来说是无限的一样,

更酷的事情是将类别名称直接加入到产品集合中,这将消耗更少的资源

so your script would be so much cooler if you

  1. get all catalog ids from all the products you have as an array
  2. load the category collection with IN() clause with the category names you need
  3. foreach your product collection one more time and select the neccecary category id's from preloaded collection with just the right elements
  4. profit $$$$ cause you are not using memory like its unlimited for you

much much cooler thing would be joining the category names directly to product collection, that would consume even less resources

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