2014-02-20 74 views
0

嗨即时尝试代码的功能,将从远程服务器导入预告片 该代码是匹配我们的magento商店中的产品与预告片服务器上的预告片。 问题在于脚本吃掉了所有的内存。有什么方法可以优化这个不消耗内存。Php脚本内存不足

这里是我的功能

function getTrailers() { 

    echo "<pre>\n"; 
    $this->_acquireLock(); 

    $_productCollection = Mage::getModel('catalog/product')->getCollection() 

     ->addAttributeToFilter('attribute_set_id', array('eq' => '9')); 
    //->addAttributeToFilter('Sku', array('eq' => '843594')); 


    $this->_logLine(sprintf("Starting import of Trailers")); 
    $i=0; 
     $server = "http://trailer.server.com/trailers"; 
     foreach($_productCollection as $product) { 
     $thispro = Mage::getModel('catalog/product')->load($product->getId()); 
     $attributeValue = $thispro->getFaktaId(); 

      //echo memory_get_usage() . "<br>\n"; 
      //echo memory_get_peak_usage() . "<br>\n"; 

     if($thispro->getMainTrailer()=="") { 
      if($attributeValue != "") { 
       $im = $attributeValue.".mp4"; 
        $url = $server.$im; 


     $exist = $this->file_exists_remote($url); 


     if($exist) { 
      $i++; 
      $product->setMainTrailer($url); 
      $product->save(); 
     } else { 

      } 
     } 
    } 
} 
    $this->_logLine(sprintf("Imported %d Trailers...", $i)); 
    $this->_releaseLock(); 

    echo "</pre>\n"; 

}

回答

1

您的主要问题是,你保持收集你的产品ver使用。其实你不需要这些产品。你只需要他们的ID。

所以,你必须编辑你这样的代码:

function getIds() { 
    return Mage::getModel('catalog/product')->getCollection() 
     ->addAttributeToFilter('attribute_set_id', array('eq' => '9')) 
     ->getAllIds(); 
} 

... 
foreach(getIds() as $id) { 
    $thispro = Mage::getModel('catalog/product')->load($id); 
    ... 
    // Further you have to replace all $product occurences with $thispro 
} 
0

多大$ _productCollection?您可以尝试批量获取产品或在for循环结束时从集合中删除产品。

1

你可以在循环重复使用相同的模型实例:

foreach($_productCollection as $product) 
{ 
    $product->load($product->getId()); 
    // ... 

    $product->clearInstance(); 
} 

甚至更​​好只加载你收集

$_productCollection = Mage::getModel('catalog/product')->getCollection() 
     ->addAttributeToFilter('attribute_set_id', array('eq' => '9')) 
     ->addAttributeToSelect(array('fakta_id', 'main_trailer')); 

,然后循环中需要的东西通过,而无需重新加载产品:

foreach($_productCollection as $product) 
{ 
    $attributeValue = $thispro->getFaktaId(); 
    // ... 
} 
+0

结束消除每一个产品它改变了内存使用量从9855568到6885536,但我想我需要比这更好的第一步 –

+0

10MB的内存使用量不适用于很多Magento的东西。你应该增加memory_limit的php配置变量到512MB –