2014-02-20 72 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 –