我有一個腳本正在同步倉庫系統與電子商務Magento的問題。Magento - PHP腳本內存限制過載
腳本正在將Magento的記錄直接更新到數據庫,價格和數量。
致命錯誤:用盡33554432個字節允許內存大小(試圖分配8309個字節)/lib/Zend/Cache/Backend/File.php上線962
這顯然內存吃腳本,有沒有任何方式來減少它的飢餓?:)
我可以增加內存容量,但它不會是解決方案,因爲那一刻有大約700MB的內存空間。
.csv文件中大約有1000行。
<?
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$count = 0;
$file = fopen(MAGENTO . '/SECRET-FOLDER-WITH-CSV/quantity-and-prices.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
if ($count == 0) {
foreach ($line as $key => $value) {
$cols[$value] = $key;
}
}
$count++;
if ($count == 1)
continue;
#Convert the lines to cols
if ($count > 0) {
foreach ($cols as $col => $value) {
unset(${$col});
${$col} = $line[$value];
}
}
// Check if SKU exists
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
if ($product) {
$productId = $product->getIdBySku($sku);
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$stockItemId = $stockItem->getId();
$stock = array();
$newprice = $line[2];
$product->setPrice($newprice)->save();
if (!$stockItemId) {
$stockItem->setData('product_id', $product->getId());
$stockItem->setData('stock_id', 1);
} else {
$stock = $stockItem->getData();
}
foreach ($cols as $col => $value) {
$stock[$col] = $line[$value];
}
foreach ($stock as $field => $value) {
$stockItem->setData($field, $value ? $value : 0);
}
$stockItem->save();
unset($stockItem);
unset($product);
}
echo "<br />Stock updated $sku";
}
fclose($file);